2017-01-22 13:46:38 +01:00
|
|
|
package org.warp.picalculator.gui.graphicengine.gpu;
|
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
import java.io.IOException;
|
2017-10-17 22:49:21 +02:00
|
|
|
import java.util.HashMap;
|
|
|
|
import java.util.Map;
|
|
|
|
import java.util.Map.Entry;
|
2017-09-23 23:23:26 +02:00
|
|
|
import java.util.concurrent.CopyOnWriteArrayList;
|
2017-09-24 13:09:30 +02:00
|
|
|
import java.util.concurrent.Semaphore;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2017-10-17 22:49:21 +02:00
|
|
|
import org.warp.picalculator.StaticVars;
|
2017-01-22 13:46:38 +01:00
|
|
|
import org.warp.picalculator.Utils;
|
2017-02-02 10:30:45 +01:00
|
|
|
import org.warp.picalculator.gui.graphicengine.BinaryFont;
|
2017-09-15 23:24:12 +02:00
|
|
|
import org.warp.picalculator.gui.graphicengine.GraphicEngine;
|
2017-05-16 22:02:44 +02:00
|
|
|
import org.warp.picalculator.gui.graphicengine.RenderingLoop;
|
2017-02-02 10:30:45 +01:00
|
|
|
import org.warp.picalculator.gui.graphicengine.Skin;
|
2017-01-31 22:29:49 +01:00
|
|
|
|
|
|
|
import com.jogamp.opengl.GLProfile;
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2017-09-23 23:23:26 +02:00
|
|
|
public class GPUEngine implements GraphicEngine {
|
2017-01-22 13:46:38 +01:00
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
private volatile boolean initialized = false;
|
|
|
|
private volatile boolean created = false;
|
|
|
|
private NEWTWindow wnd;
|
2017-02-02 10:30:45 +01:00
|
|
|
private RenderingLoop d;
|
2017-01-22 13:46:38 +01:00
|
|
|
private GPURenderer r;
|
2017-10-17 22:49:21 +02:00
|
|
|
private Map<String, GPUFont> fontCache = new HashMap<String, GPUFont>();
|
|
|
|
int[] size = new int[] { StaticVars.screenSize[0], StaticVars.screenSize[1] };
|
2017-09-23 23:23:26 +02:00
|
|
|
private final CopyOnWriteArrayList<BinaryFont> registeredFonts = new CopyOnWriteArrayList<BinaryFont>();
|
2017-09-24 13:09:30 +02:00
|
|
|
private Semaphore exitSemaphore = new Semaphore(0);
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-22 13:46:38 +01:00
|
|
|
@Override
|
|
|
|
public int[] getSize() {
|
2017-01-31 22:29:49 +01:00
|
|
|
return size;
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isInitialized() {
|
|
|
|
return initialized;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setTitle(String title) {
|
2017-01-31 22:29:49 +01:00
|
|
|
wnd.window.setTitle(title);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setResizable(boolean r) {
|
|
|
|
if (!r) {
|
|
|
|
wnd.window.setPosition(0, 0);
|
|
|
|
}
|
|
|
|
wnd.window.setResizable(r);
|
|
|
|
wnd.window.setUndecorated(!r);
|
2017-01-31 22:29:49 +01:00
|
|
|
wnd.window.setPointerVisible(r);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void setDisplayMode(int ww, int wh) {
|
2017-04-10 22:50:43 +02:00
|
|
|
size[0] = ww;
|
|
|
|
size[1] = wh;
|
2017-10-17 22:49:21 +02:00
|
|
|
wnd.window.setSize((StaticVars.debugOn & StaticVars.debugWindow2x) ? ww * 2 : ww, (StaticVars.debugOn & StaticVars.debugWindow2x) ? wh * 2 : wh);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void create() {
|
2017-09-15 23:24:12 +02:00
|
|
|
create(null);
|
|
|
|
}
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-15 23:24:12 +02:00
|
|
|
@Override
|
|
|
|
public void create(Runnable onInitialized) {
|
2017-01-31 22:29:49 +01:00
|
|
|
created = true;
|
2017-01-22 13:46:38 +01:00
|
|
|
r = new GPURenderer();
|
2017-01-31 22:29:49 +01:00
|
|
|
wnd = new NEWTWindow(this);
|
2017-01-22 13:46:38 +01:00
|
|
|
wnd.create();
|
2017-10-17 22:49:21 +02:00
|
|
|
setDisplayMode(StaticVars.screenSize[0], StaticVars.screenSize[1]);
|
|
|
|
setResizable(StaticVars.debugOn & !Utils.debugThirdScreen);
|
2017-01-22 13:46:38 +01:00
|
|
|
initialized = true;
|
2017-09-15 23:24:12 +02:00
|
|
|
wnd.onInitialized = onInitialized;
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean wasResized() {
|
2017-10-17 22:49:21 +02:00
|
|
|
return StaticVars.screenSize[0] != size[0] | StaticVars.screenSize[1] != size[1];
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getWidth() {
|
2017-01-31 22:29:49 +01:00
|
|
|
return size[0];
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int getHeight() {
|
2017-01-31 22:29:49 +01:00
|
|
|
return size[1];
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void destroy() {
|
2017-01-31 22:29:49 +01:00
|
|
|
initialized = false;
|
|
|
|
created = false;
|
2017-09-24 13:09:30 +02:00
|
|
|
exitSemaphore.release();
|
2017-01-31 22:29:49 +01:00
|
|
|
wnd.window.destroy();
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-02 10:30:45 +01:00
|
|
|
public void start(RenderingLoop d) {
|
2017-01-31 22:29:49 +01:00
|
|
|
this.d = d;
|
|
|
|
wnd.window.setVisible(true);
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void repaint() {
|
2017-04-10 22:50:43 +02:00
|
|
|
if (d != null & r != null && GPURenderer.gl != null) {
|
2017-01-22 13:46:38 +01:00
|
|
|
d.refresh();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public GPURenderer getRenderer() {
|
|
|
|
return r;
|
|
|
|
}
|
|
|
|
|
2017-01-31 22:29:49 +01:00
|
|
|
@Override
|
2017-11-19 22:58:37 +01:00
|
|
|
public BinaryFont loadFont(String name) throws IOException {
|
2017-10-17 22:49:21 +02:00
|
|
|
for (Entry<String, GPUFont> entry : fontCache.entrySet()) {
|
2017-11-19 22:58:37 +01:00
|
|
|
if (entry.getKey().equals(name)) {
|
2017-10-17 22:49:21 +02:00
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
2017-11-19 22:58:37 +01:00
|
|
|
GPUFont font = new GPUFont(this, name);
|
|
|
|
fontCache.put(name, font);
|
|
|
|
return font;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public BinaryFont loadFont(String path, String name) throws IOException {
|
|
|
|
for (Entry<String, GPUFont> entry : fontCache.entrySet()) {
|
|
|
|
if (entry.getKey().equals(name)) {
|
|
|
|
return entry.getValue();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
GPUFont font = new GPUFont(this, path, name);
|
|
|
|
fontCache.put(name, font);
|
2017-10-17 22:49:21 +02:00
|
|
|
return font;
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-02-02 10:30:45 +01:00
|
|
|
public Skin loadSkin(String file) throws IOException {
|
2017-09-15 23:24:12 +02:00
|
|
|
return new GPUSkin(this, file);
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-24 18:47:30 +02:00
|
|
|
public void waitForExit() {
|
2017-01-31 22:29:49 +01:00
|
|
|
try {
|
2017-09-24 13:09:30 +02:00
|
|
|
exitSemaphore.acquire();
|
2017-09-24 18:47:30 +02:00
|
|
|
} catch (InterruptedException e) {}
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean isSupported() {
|
2017-09-24 18:47:30 +02:00
|
|
|
if (Utils.forceEngine != null && Utils.forceEngine != "gpu")
|
|
|
|
return false;
|
|
|
|
if (Utils.headlessOverride)
|
|
|
|
return false;
|
2017-05-01 22:30:39 +02:00
|
|
|
boolean available = false;
|
|
|
|
boolean errored = false;
|
|
|
|
try {
|
2017-10-17 22:49:21 +02:00
|
|
|
available = GLProfile.isAvailable(GLProfile.GL2ES2);
|
2017-09-24 18:47:30 +02:00
|
|
|
} catch (Exception ex) {
|
|
|
|
errored = true;
|
2017-12-09 23:00:04 +01:00
|
|
|
System.err.println("OpenGL Error: "+ex.getMessage());
|
2017-09-24 18:47:30 +02:00
|
|
|
}
|
2017-05-01 22:30:39 +02:00
|
|
|
if (!available && !errored) {
|
2017-02-02 18:47:03 +01:00
|
|
|
System.err.println(GLProfile.glAvailabilityToString());
|
|
|
|
}
|
2017-02-10 18:12:17 +01:00
|
|
|
return available;
|
2017-02-06 21:38:17 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean doesRefreshPauses() {
|
|
|
|
return false;
|
2017-01-31 22:29:49 +01:00
|
|
|
}
|
2017-09-24 18:47:30 +02:00
|
|
|
|
2017-09-15 23:24:12 +02:00
|
|
|
public void registerFont(GPUFont gpuFont) {
|
|
|
|
registeredFonts.add(gpuFont);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean supportsFontRegistering() {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
2017-09-23 23:23:26 +02:00
|
|
|
public CopyOnWriteArrayList<BinaryFont> getRegisteredFonts() {
|
2017-09-15 23:24:12 +02:00
|
|
|
return registeredFonts;
|
|
|
|
}
|
2017-01-31 22:29:49 +01:00
|
|
|
|
2017-01-22 13:46:38 +01:00
|
|
|
}
|