mirror of
https://github.com/AngelAuraMC/Amethyst-Android.git
synced 2025-09-18 00:59:35 -04:00
Refactor AWTCanvasView.java
This commit is contained in:
parent
3618bb6310
commit
534e244ba5
@ -10,56 +10,20 @@ import net.kdt.pojavlaunch.utils.*;
|
|||||||
import org.lwjgl.glfw.*;
|
import org.lwjgl.glfw.*;
|
||||||
|
|
||||||
public class AWTCanvasView extends TextureView implements TextureView.SurfaceTextureListener, Runnable {
|
public class AWTCanvasView extends TextureView implements TextureView.SurfaceTextureListener, Runnable {
|
||||||
|
private final int MAX_SIZE = 100;
|
||||||
|
private final double NANOS = 1000000000.0;
|
||||||
|
|
||||||
private int mScaleFactor;
|
private int mScaleFactor;
|
||||||
private int[] mScales;
|
private int[] mScales;
|
||||||
|
|
||||||
private int mWidth, mHeight;
|
private int mWidth, mHeight;
|
||||||
private boolean mIsDestroyed = false;
|
private boolean mIsDestroyed = false;
|
||||||
|
private final TextPaint mFpsPaint;
|
||||||
private TextPaint fpsPaint;
|
private boolean mAttached = false;
|
||||||
private boolean attached = false;
|
private boolean mDrawing;
|
||||||
|
|
||||||
// Temporary count fps https://stackoverflow.com/a/13729241
|
// Temporary count fps https://stackoverflow.com/a/13729241
|
||||||
private LinkedList<Long> times = new LinkedList<Long>(){{add(System.nanoTime());}};
|
private final LinkedList<Long> mTimes = new LinkedList<Long>(){{add(System.nanoTime());}};
|
||||||
private final int MAX_SIZE = 100;
|
|
||||||
private final double NANOS = 1000000000.0;
|
|
||||||
|
|
||||||
/** Calculates and returns frames per second */
|
|
||||||
private double fps() {
|
|
||||||
long lastTime = System.nanoTime();
|
|
||||||
double difference = (lastTime - times.getFirst()) / NANOS;
|
|
||||||
times.addLast(lastTime);
|
|
||||||
int size = times.size();
|
|
||||||
if (size > MAX_SIZE) {
|
|
||||||
times.removeFirst();
|
|
||||||
}
|
|
||||||
return difference > 0 ? times.size() / difference : 0.0;
|
|
||||||
}
|
|
||||||
|
|
||||||
/** Computes the scale to better fit the screen */
|
|
||||||
void initScaleFactors(){
|
|
||||||
initScaleFactors(0);
|
|
||||||
}
|
|
||||||
|
|
||||||
void initScaleFactors(int forcedScale){
|
|
||||||
//Could be optimized
|
|
||||||
if(forcedScale < 1) { //Auto scale
|
|
||||||
int minDimension = Math.min(CallbackBridge.physicalHeight, CallbackBridge.physicalWidth);
|
|
||||||
mScaleFactor = Math.max(((3 * minDimension) / 1080) - 1, 1);
|
|
||||||
}else{
|
|
||||||
mScaleFactor = forcedScale;
|
|
||||||
}
|
|
||||||
|
|
||||||
int[] scales = new int[2]; //Left, Top
|
|
||||||
|
|
||||||
scales[0] = (CallbackBridge.physicalWidth/2);
|
|
||||||
scales[0] -= scales[0]/mScaleFactor;
|
|
||||||
|
|
||||||
scales[1] = (CallbackBridge.physicalHeight/2);
|
|
||||||
scales[1] -= scales[1]/mScaleFactor;
|
|
||||||
|
|
||||||
mScales = scales;
|
|
||||||
}
|
|
||||||
|
|
||||||
public AWTCanvasView(Context ctx) {
|
public AWTCanvasView(Context ctx) {
|
||||||
this(ctx, null);
|
this(ctx, null);
|
||||||
@ -67,11 +31,10 @@ public class AWTCanvasView extends TextureView implements TextureView.SurfaceTex
|
|||||||
|
|
||||||
public AWTCanvasView(Context ctx, AttributeSet attrs) {
|
public AWTCanvasView(Context ctx, AttributeSet attrs) {
|
||||||
super(ctx, attrs);
|
super(ctx, attrs);
|
||||||
// setWillNotDraw(false);
|
|
||||||
|
|
||||||
fpsPaint = new TextPaint();
|
mFpsPaint = new TextPaint();
|
||||||
fpsPaint.setColor(Color.WHITE);
|
mFpsPaint.setColor(Color.WHITE);
|
||||||
fpsPaint.setTextSize(20);
|
mFpsPaint.setTextSize(20);
|
||||||
|
|
||||||
setSurfaceTextureListener(this);
|
setSurfaceTextureListener(this);
|
||||||
initScaleFactors();
|
initScaleFactors();
|
||||||
@ -102,20 +65,18 @@ public class AWTCanvasView extends TextureView implements TextureView.SurfaceTex
|
|||||||
public void onSurfaceTextureUpdated(SurfaceTexture texture) {
|
public void onSurfaceTextureUpdated(SurfaceTexture texture) {
|
||||||
}
|
}
|
||||||
|
|
||||||
private boolean mDrawing;
|
|
||||||
private Surface mSurface;
|
|
||||||
@Override
|
@Override
|
||||||
public void run() {
|
public void run() {
|
||||||
Canvas canvas;
|
Canvas canvas;
|
||||||
mSurface = new Surface(getSurfaceTexture());
|
Surface surface = new Surface(getSurfaceTexture());
|
||||||
|
|
||||||
try {
|
try {
|
||||||
while (!mIsDestroyed && mSurface.isValid()) {
|
while (!mIsDestroyed && surface.isValid()) {
|
||||||
canvas = mSurface.lockCanvas(null);
|
canvas = surface.lockCanvas(null);
|
||||||
canvas.drawRGB(0, 0, 0);
|
canvas.drawRGB(0, 0, 0);
|
||||||
|
|
||||||
if (!attached) {
|
if (!mAttached) {
|
||||||
attached = CallbackBridge.nativeAttachThreadToOther(true, BaseMainActivity.isInputStackCall);
|
mAttached = CallbackBridge.nativeAttachThreadToOther(true, BaseMainActivity.isInputStackCall);
|
||||||
} else {
|
} else {
|
||||||
int[] rgbArray = JREUtils.renderAWTScreenFrame(/* canvas, mWidth, mHeight */);
|
int[] rgbArray = JREUtils.renderAWTScreenFrame(/* canvas, mWidth, mHeight */);
|
||||||
mDrawing = rgbArray != null;
|
mDrawing = rgbArray != null;
|
||||||
@ -123,22 +84,55 @@ public class AWTCanvasView extends TextureView implements TextureView.SurfaceTex
|
|||||||
|
|
||||||
canvas.save();
|
canvas.save();
|
||||||
canvas.scale(mScaleFactor, mScaleFactor);
|
canvas.scale(mScaleFactor, mScaleFactor);
|
||||||
canvas.translate(-mScales[0],-mScales[1]);
|
canvas.translate(-mScales[0], -mScales[1]);
|
||||||
|
|
||||||
|
|
||||||
canvas.drawBitmap(rgbArray, 0, CallbackBridge.physicalWidth, 0, 0, CallbackBridge.physicalWidth, CallbackBridge.physicalHeight, true, null);
|
canvas.drawBitmap(rgbArray, 0, CallbackBridge.physicalWidth, 0, 0, CallbackBridge.physicalWidth, CallbackBridge.physicalHeight, true, null);
|
||||||
canvas.restore();
|
canvas.restore();
|
||||||
|
|
||||||
}
|
}
|
||||||
rgbArray = null;
|
|
||||||
// System.gc();
|
|
||||||
}
|
}
|
||||||
canvas.drawText("FPS: " + (Math.round(fps() * 10) / 10) + ", attached=" + attached + ", drawing=" + mDrawing, 50, 50, fpsPaint);
|
canvas.drawText("FPS: " + (Math.round(fps() * 10) / 10) + ", attached=" + mAttached + ", drawing=" + mDrawing, 50, 50, mFpsPaint);
|
||||||
|
surface.unlockCanvasAndPost(canvas);
|
||||||
mSurface.unlockCanvasAndPost(canvas);
|
|
||||||
}
|
}
|
||||||
} catch (Throwable th) {
|
} catch (Throwable throwable) {
|
||||||
Tools.showError(getContext(), th);
|
Tools.showError(getContext(), throwable);
|
||||||
}
|
}
|
||||||
|
surface.release();
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Computes the scale to better fit the screen */
|
||||||
|
void initScaleFactors(){
|
||||||
|
initScaleFactors(0);
|
||||||
|
}
|
||||||
|
|
||||||
|
void initScaleFactors(int forcedScale){
|
||||||
|
//Could be optimized
|
||||||
|
if(forcedScale < 1) { //Auto scale
|
||||||
|
int minDimension = Math.min(CallbackBridge.physicalHeight, CallbackBridge.physicalWidth);
|
||||||
|
mScaleFactor = Math.max(((3 * minDimension) / 1080) - 1, 1);
|
||||||
|
}else{
|
||||||
|
mScaleFactor = forcedScale;
|
||||||
|
}
|
||||||
|
|
||||||
|
int[] scales = new int[2]; //Left, Top
|
||||||
|
|
||||||
|
scales[0] = (CallbackBridge.physicalWidth/2);
|
||||||
|
scales[0] -= scales[0]/mScaleFactor;
|
||||||
|
|
||||||
|
scales[1] = (CallbackBridge.physicalHeight/2);
|
||||||
|
scales[1] -= scales[1]/mScaleFactor;
|
||||||
|
|
||||||
|
mScales = scales;
|
||||||
|
}
|
||||||
|
|
||||||
|
/** Calculates and returns frames per second */
|
||||||
|
private double fps() {
|
||||||
|
long lastTime = System.nanoTime();
|
||||||
|
double difference = (lastTime - mTimes.getFirst()) / NANOS;
|
||||||
|
mTimes.addLast(lastTime);
|
||||||
|
int size = mTimes.size();
|
||||||
|
if (size > MAX_SIZE) {
|
||||||
|
mTimes.removeFirst();
|
||||||
|
}
|
||||||
|
return difference > 0 ? mTimes.size() / difference : 0.0;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
Loading…
x
Reference in New Issue
Block a user