gpt4 book ai didi

Java缓冲策略失去硬件加速

转载 作者:行者123 更新时间:2023-11-30 11:45:40 24 4
gpt4 key购买 nike

我正在开发 Java 游戏引擎,当我切换到 Windows 登录屏幕并返回时,缓冲策略失去了它的硬件加速。在关闭程序并重新启动之前,我所做的任何事情都不会将缓冲区恢复为硬件加速。

下面是程序启动时 ((Graphics2D) bufferStrategy.getDrawGraphics()).getDeviceConfiguration() 的一些属性。

Bounds: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DBufferCaps@114b82b
Back Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@147358f
Accelerated: true
True Volatile: true
Flip Contents: undefined
Front Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@147358f
Accelerated: true
True Volatile: true
Is Full Screen Required: false
Is MultiBuffer Available: true
Is Page Flipping: true
Device: D3DGraphicsDevice[screen=1]
Available Accelerated Memory: 750780416
ID String: \Display1
Type: 0
Display Mode: java.awt.DisplayMode@dd3
Image Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@147358f
Accelerated: true
True Volatile: true

这是显示登录屏幕后的相同属性。

Bounds: java.awt.Rectangle[x=0,y=0,width=942,height=566]
Buffer Capabilities: java.awt.GraphicsConfiguration$DefaultBufferCapabilities@19d688
Back Buffer Capabilities: java.awt.ImageCapabilities@539a92
Accelerated: false
True Volatile: false
Flip Contents: null
Front Buffer Capabilities: java.awt.ImageCapabilities@539a92
Accelerated: false
True Volatile: false
Is Full Screen Required: false
Is MultiBuffer Available: false
Is Page Flipping: false
Device: sun.awt.image.BufferedImageDevice@c8f0a4
Available Accelerated Memory: -1
ID String: BufferedImage
Type: 2
Display Mode: java.awt.DisplayMode@68c
Image Capabilities: java.awt.ImageCapabilities@539a92
Accelerated: false
True Volatile: false

缓冲区功能从 sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@147358f 更改为 java.awt.GraphicsConfiguration$DefaultBufferCapabilities@19d688。如果还没有找到将缓冲策略返回到硬件加速的方法。重新配置和创建缓冲策略不会恢复硬件加速。

缓冲区策略是在 Canvas 对象上创建的。上面的 ((Graphics2D) bufferStrategy.getDrawGraphics()).getDeviceConfiguration() 属性失去了加速,但是当我通过调用 canvas.getGraphicsConfiguration() 获取 Canvas 对象上的 GraphicsConfiguration 时,Canvas 对象本身仍然显示它已加速。

以下是 canvas.getGraphicsConfiguration() 的属性,而缓冲策略中的图形对象为加速返回 false。

Bounds: java.awt.Rectangle[x=0,y=0,width=1440,height=900]
Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DBufferCaps@1672113
Back Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@d5eb7
Accelerated: true
True Volatile: true
Flip Contents: undefined
Front Buffer Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@d5eb7
Accelerated: true
True Volatile: true
Is Full Screen Required: false
Is MultiBuffer Available: true
Is Page Flipping: true
Device: D3DGraphicsDevice[screen=1]
Available Accelerated Memory: 764411904
ID String: \Display1
Type: 0
Display Mode: java.awt.DisplayMode@dd3
Image Capabilities: sun.java2d.d3d.D3DGraphicsConfig$D3DImageCaps@d5eb7
Accelerated: true
True Volatile: true

以下是可用于重现上述问题的代码:

import java.awt.Color;
import java.awt.EventQueue;
import java.awt.Graphics2D;
import java.awt.GraphicsConfiguration;
import java.awt.image.BufferStrategy;

import javax.swing.JFrame;

public class AccelerationTest extends JFrame {

public AccelerationTest(){
setSize(500, 500);
setVisible(true);
setIgnoreRepaint(true);
createBufferStrategy(2);

setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

(new Thread(new Runnable(){
public void run(){
do{
render();
try{Thread.sleep(200);}catch(InterruptedException e){}
}while (true);
}
})).start();
}

public static void main(String args[]) {
EventQueue.invokeLater(new Runnable() {
public void run() {
new AccelerationTest();
}
});
}

private void render(){

BufferStrategy bufferStrategy = getBufferStrategy();
Graphics2D graphic = (Graphics2D) bufferStrategy.getDrawGraphics();

graphic.setColor(Color.red);
graphic.fillRect(0, 0, getWidth(), getHeight());
graphic.setColor(Color.white);
graphic.fillRect((int) (getWidth() * 0.1), (int) (getHeight() * 0.1), (int) (getWidth() * 0.8), (int) (getHeight() * 0.8));

bufferStrategy.show();
System.out.println();
System.out.println("graphic from frame: \n" + getDeviceConfigurationString(graphic.getDeviceConfiguration()));
System.out.println();
System.out.println("frame: \n" + getDeviceConfigurationString(getGraphicsConfiguration()));

graphic.dispose();
}

private String getDeviceConfigurationString(GraphicsConfiguration gc){
return "Bounds: " + gc.getBounds() + "\n" +
"Buffer Capabilities: " + gc.getBufferCapabilities() + "\n" +
" Back Buffer Capabilities: " + gc.getBufferCapabilities().getBackBufferCapabilities() + "\n" +
" Accelerated: " + gc.getBufferCapabilities().getBackBufferCapabilities().isAccelerated() + "\n" +
" True Volatile: " + gc.getBufferCapabilities().getBackBufferCapabilities().isTrueVolatile() + "\n" +
" Flip Contents: " + gc.getBufferCapabilities().getFlipContents() + "\n" +
" Front Buffer Capabilities: " + gc.getBufferCapabilities().getFrontBufferCapabilities() + "\n" +
" Accelerated: " + gc.getBufferCapabilities().getFrontBufferCapabilities().isAccelerated() + "\n" +
" True Volatile: " + gc.getBufferCapabilities().getFrontBufferCapabilities().isTrueVolatile() + "\n" +
" Is Full Screen Required: " + gc.getBufferCapabilities().isFullScreenRequired() + "\n" +
" Is MultiBuffer Available: " + gc.getBufferCapabilities().isMultiBufferAvailable() + "\n" +
" Is Page Flipping: " + gc.getBufferCapabilities().isPageFlipping() + "\n" +
"Device: " + gc.getDevice() + "\n" +
" Available Accelerated Memory: " + gc.getDevice().getAvailableAcceleratedMemory() + "\n" +
" ID String: " + gc.getDevice().getIDstring() + "\n" +
" Type: " + gc.getDevice().getType() + "\n" +
" Display Mode: " + gc.getDevice().getDisplayMode() + "\n" +
"Image Capabilities: " + gc.getImageCapabilities() + "\n" +
" Accelerated: " + gc.getImageCapabilities().isAccelerated() + "\n" +
" True Volatile: " + gc.getImageCapabilities().isTrueVolatile() + "\n";
}
}

最佳答案

这个问题可以忽略。在项目外测试代码后,我发现失去硬件加速的不是缓冲策略,而是一个VolatileImage。在检查是否需要重新创建 VolatileImage 时,如果 isAccelerated 返回 false,则跳过该部分。因此在屏幕切换期间,加速似乎不可用但再次可用但代码跳过了该部分,因为创建的 VolatileImage 已经在没有加速的情况下创建。

关于Java缓冲策略失去硬件加速,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10256180/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com