Skip to content

Commit

Permalink
Separate view for graphics mode GLES
Browse files Browse the repository at this point in the history
  • Loading branch information
woesss committed Sep 15, 2020
1 parent 90c232a commit 18c0293
Show file tree
Hide file tree
Showing 4 changed files with 199 additions and 83 deletions.
4 changes: 2 additions & 2 deletions app/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,8 @@ android {
applicationId "ru.woesss.j2meloader"
minSdkVersion minSdk
targetSdkVersion targetSdk
versionCode 14
versionName "0.74"
versionCode 17
versionName "0.75"
archivesBaseName = "JL-Mod_$versionName"
ndk {
abiFilters 'x86', 'armeabi-v7a'
Expand Down
187 changes: 106 additions & 81 deletions app/src/main/java/javax/microedition/lcdui/Canvas.java
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,6 @@

import android.annotation.SuppressLint;
import android.app.Activity;
import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.PixelFormat;
import android.graphics.Rect;
Expand All @@ -35,6 +34,7 @@
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.view.View;
import android.widget.FrameLayout;
import android.widget.LinearLayout;
Expand All @@ -49,12 +49,15 @@
import javax.microedition.lcdui.event.Event;
import javax.microedition.lcdui.event.EventFilter;
import javax.microedition.lcdui.event.EventQueue;
import javax.microedition.lcdui.graphics.CanvasView;
import javax.microedition.lcdui.graphics.CanvasWrapper;
import javax.microedition.lcdui.graphics.GlesView;
import javax.microedition.lcdui.graphics.ShaderProgram;
import javax.microedition.lcdui.overlay.FpsCounter;
import javax.microedition.lcdui.overlay.Overlay;
import javax.microedition.lcdui.overlay.OverlayView;
import javax.microedition.lcdui.pointer.FixedKeyboard;
import javax.microedition.shell.MicroActivity;
import javax.microedition.util.ContextHolder;

import androidx.annotation.NonNull;
Expand All @@ -63,6 +66,7 @@
import ru.playsoftware.j2meloader.config.ShaderInfo;

import static android.opengl.GLES20.*;
import static android.opengl.GLSurfaceView.RENDERMODE_WHEN_DIRTY;

@SuppressWarnings({"WeakerAccess", "unused"})
public abstract class Canvas extends Displayable {
Expand Down Expand Up @@ -179,14 +183,13 @@ public abstract class Canvas extends Displayable {

private final Object paintSync = new Object();
private final PaintEvent paintEvent = new PaintEvent();
private final FloatBuffer VERTEX_BG = ByteBuffer.allocateDirect(8 * 2 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
protected int width, height;
protected int maxHeight;
private LinearLayout layout;
private InnerView innerView;
private SurfaceView innerView;
private Surface surface;
private CanvasWrapper canvasWrapper = new CanvasWrapper(filter);
private GLRenderer renderer;
private int displayWidth;
private int displayHeight;
private boolean fullscreen = forceFullscreen;
Expand All @@ -200,9 +203,11 @@ public abstract class Canvas extends Displayable {
private Handler uiHandler;
private Overlay overlay;
private FpsCounter fpsCounter;
private boolean renderStarted;

public Canvas() {
if (graphicsMode == 1) {
renderer = new GLRenderer();
}
if (parallelRedraw) {
uiHandler = new Handler(Looper.getMainLooper(), msg -> repaintScreen());
}
Expand Down Expand Up @@ -373,6 +378,18 @@ public void callHideNotify() {
hideNotify();
}

public void onDraw(android.graphics.Canvas canvas) {
if (graphicsMode != 2) return; // Fix for Android Pie
CanvasWrapper g = canvasWrapper;
g.bind(canvas);
g.clear(backgroundColor);
offscreenCopy.getBitmap().prepareToDraw();
g.drawImage(offscreenCopy, virtualScreen);
if (fpsCounter != null) {
fpsCounter.increment();
}
}

public void setOverlay(Overlay ov) {
if (ov != null) {
ov.setTarget(this);
Expand Down Expand Up @@ -536,15 +553,7 @@ private void updateSize() {
float gb = 1.0f - 2.0f * virtualScreen.bottom / displayHeight;
float th = (float) height / offscreen.getBitmap().getHeight();
float tw = (float) width / offscreen.getBitmap().getWidth();
FloatBuffer vertex_bg = VERTEX_BG;
vertex_bg.rewind();
vertex_bg.put(gl).put(gt).put(0.0f).put(0.0f);// lt
vertex_bg.put(gl).put(gb).put(0.0f).put( th);// lb
vertex_bg.put(gr).put(gt).put( tw).put(0.0f);// rt
vertex_bg.put(gr).put(gb).put( tw).put( th);// rb
if (renderStarted) {
innerView.updateSize();
}
renderer.updateSize(gl, gt, gr, gb, th, tw);
}
}

Expand All @@ -568,11 +577,31 @@ private float convertPointerY(float y) {
return (y - onY) * height / onHeight;
}

@SuppressLint("ClickableViewAccessibility")
@Override
public View getDisplayableView() {
if (layout == null) {
layout = (LinearLayout) super.getDisplayableView();
innerView = new InnerView(getParentActivity());
MicroActivity activity = getParentActivity();
if (graphicsMode == 1) {
GlesView glesView = new GlesView(activity);
glesView.setRenderer(renderer);
glesView.setRenderMode(RENDERMODE_WHEN_DIRTY);
renderer.setView(glesView);
innerView = glesView;
} else {
CanvasView canvasView = new CanvasView(this, activity);
if (graphicsMode == 2) {
canvasView.setWillNotDraw(false);
}
canvasView.getHolder().setFormat(PixelFormat.RGBA_8888);
innerView = canvasView;
}
ViewCallbacks callback = new ViewCallbacks(innerView);
innerView.getHolder().addCallback(callback);
innerView.setOnTouchListener(callback);
innerView.setOnKeyListener(callback);
innerView.setFocusableInTouchMode(true);
layout.addView(innerView);
}
return layout;
Expand Down Expand Up @@ -647,7 +676,7 @@ public void flushBuffer(Image image, int x, int y, int width, int height) {
offscreenCopy.getSingleGraphics().flush(image, x, y, width, height);
if (graphicsMode == 1) {
if (innerView != null) {
innerView.requestRender();
renderer.requestRender();
}
return;
} else if (graphicsMode == 2) {
Expand All @@ -671,7 +700,7 @@ public void flushBuffer(Image image, int x, int y) {
image.copyTo(offscreenCopy, x, y);
if (graphicsMode == 1) {
if (innerView != null) {
innerView.requestRender();
renderer.requestRender();
}
return;
} else if (graphicsMode == 2) {
Expand Down Expand Up @@ -825,8 +854,12 @@ public void pointerReleased(int x, int y) {
}

private class GLRenderer implements GLSurfaceView.Renderer {
private final FloatBuffer vbo = ByteBuffer.allocateDirect(8 * 2 * 4)
.order(ByteOrder.nativeOrder()).asFloatBuffer();
private GLSurfaceView mView;
private int[] bgTextureId = new int[1];
private ShaderProgram program;
private boolean isStarted;

@Override
public void onSurfaceCreated(GL10 gl, EGLConfig config) {
Expand All @@ -838,11 +871,11 @@ public void onSurfaceCreated(GL10 gl, EGLConfig config) {
glDepthMask(false);
initTex();
Bitmap bitmap = offscreenCopy.getBitmap();
program.loadVbo(VERTEX_BG, bitmap.getWidth(), bitmap.getHeight());
program.loadVbo(vbo, bitmap.getWidth(), bitmap.getHeight());
if (shaderFilter.values != null) {
glUniform4fv(program.uSetting, 1, shaderFilter.values, 0);
}
renderStarted = true;
isStarted = true;
}

@Override
Expand Down Expand Up @@ -874,6 +907,37 @@ private void initTex() {
glUniform1i(program.uTextureUnit, 0);
}

public void updateSize(float gl, float gt, float gr, float gb, float th, float tw) {
FloatBuffer vertex_bg = vbo;
vertex_bg.rewind();
vertex_bg.put(gl).put(gt).put(0.0f).put(0.0f);// lt
vertex_bg.put(gl).put(gb).put(0.0f).put( th);// lb
vertex_bg.put(gr).put(gt).put( tw).put(0.0f);// rt
vertex_bg.put(gr).put(gb).put( tw).put( th);// rb
if (isStarted) {
mView.queueEvent(() -> {
Bitmap bitmap = offscreenCopy.getBitmap();
program.loadVbo(vbo, bitmap.getWidth(), bitmap.getHeight());
});
}
}

public void requestRender() {
mView.requestRender();
}

public void setView(GLSurfaceView mView) {
this.mView = mView;
}

public void stop() {
isStarted = false;
mView.onPause();
}

public void start() {
mView.onResume();
}
}

private class PaintEvent extends Event implements EventFilter {
Expand All @@ -896,7 +960,7 @@ public void process() {
offscreen.copyTo(offscreenCopy);
if (graphicsMode == 1) {
if (innerView != null) {
innerView.requestRender();
renderer.requestRender();
}
} else if (graphicsMode == 2) {
if (innerView != null) {
Expand Down Expand Up @@ -941,42 +1005,29 @@ public boolean accept(Event event) {
}
}

private class InnerView extends GLSurfaceView {
private class ViewCallbacks implements View.OnTouchListener, SurfaceHolder.Callback, View.OnKeyListener {

private final View mView;
OverlayView overlayView;
private GLRenderer renderer;
private FrameLayout rootView;

public InnerView(Context context) {
super(context);
if (graphicsMode == 1) {
setEGLContextClientVersion(2);
setEGLConfigChooser(false);
renderer = new GLRenderer();
setRenderer(renderer);
setRenderMode(RENDERMODE_WHEN_DIRTY);
setWillNotDraw(true);
} else {
if (graphicsMode == 2) {
setWillNotDraw(false);
}
getHolder().setFormat(PixelFormat.RGBA_8888);
}
rootView = ((Activity) context).findViewById(R.id.midletFrame);
public ViewCallbacks(View view) {
mView = view;
rootView = ((Activity) view.getContext()).findViewById(R.id.midletFrame);
overlayView = rootView.findViewById(R.id.vOverlay);
setFocusableInTouchMode(true);
}

@Override
protected void onVisibilityChanged(@NonNull View changedView, int visibility) {
super.onVisibilityChanged(changedView, visibility);
// Fix keyboard issue on Blackberry
if (visibility == VISIBLE) {
requestFocus();
public boolean onKey(View v, int keyCode, KeyEvent event) {
switch (event.getAction()) {
case KeyEvent.ACTION_DOWN:
return onKeyDown(keyCode, event);
case KeyEvent.ACTION_UP:
return onKeyUp(keyCode, event);
}
return false;
}

@Override
public boolean onKeyDown(int keyCode, KeyEvent event) {
keyCode = convertAndroidKeyCode(keyCode);
if (keyCode == Integer.MAX_VALUE) {
Expand All @@ -994,7 +1045,6 @@ public boolean onKeyDown(int keyCode, KeyEvent event) {
return true;
}

@Override
public boolean onKeyUp(int keyCode, KeyEvent event) {
keyCode = convertAndroidKeyCode(keyCode);
if (keyCode == Integer.MAX_VALUE) {
Expand All @@ -1008,7 +1058,7 @@ public boolean onKeyUp(int keyCode, KeyEvent event) {

@Override
@SuppressLint("ClickableViewAccessibility")
public boolean onTouchEvent(MotionEvent event) {
public boolean onTouch(View v, MotionEvent event) {
switch (event.getActionMasked()) {
case MotionEvent.ACTION_DOWN:
if (overlay != null) {
Expand Down Expand Up @@ -1064,19 +1114,16 @@ public boolean onTouchEvent(MotionEvent event) {
}
break;
default:
return super.onTouchEvent(event);
return false;
}
return true;
}

@Override
public void surfaceChanged(SurfaceHolder holder, int format, int newWidth, int newHeight) {
if (graphicsMode == 1) {
super.surfaceChanged(holder, format, newWidth, newHeight);
}
public void surfaceChanged(@NonNull SurfaceHolder holder, int format, int newWidth, int newHeight) {
Rect offsetViewBounds = new Rect(0, 0, newWidth, newHeight);
// calculates the relative coordinates to the parent
rootView.offsetDescendantRectToMyCoords(this, offsetViewBounds);
rootView.offsetDescendantRectToMyCoords(mView, offsetViewBounds);
synchronized (paintSync) {
overlayView.setTargetBounds(offsetViewBounds);
displayWidth = newWidth;
Expand All @@ -1091,10 +1138,9 @@ public void surfaceChanged(SurfaceHolder holder, int format, int newWidth, int n
}

@Override
public void surfaceCreated(SurfaceHolder holder) {
if (graphicsMode == 1) {
super.surfaceCreated(holder);
onResume();
public void surfaceCreated(@NonNull SurfaceHolder holder) {
if (renderer != null) {
renderer.start();
}
synchronized (paintSync) {
surface = holder.getSurface();
Expand All @@ -1108,11 +1154,9 @@ public void surfaceCreated(SurfaceHolder holder) {
}

@Override
public void surfaceDestroyed(SurfaceHolder holder) {
renderStarted = false;
if (graphicsMode == 1) {
onPause();
super.surfaceDestroyed(holder);
public void surfaceDestroyed(@NonNull SurfaceHolder holder) {
if (renderer != null) {
renderer.stop();
}
synchronized (paintSync) {
surface = null;
Expand All @@ -1126,24 +1170,5 @@ public void surfaceDestroyed(SurfaceHolder holder) {
overlayView.setVisibility(false);
}

public void updateSize() {
queueEvent(() -> {
Bitmap bitmap = offscreenCopy.getBitmap();
renderer.program.loadVbo(VERTEX_BG, bitmap.getWidth(), bitmap.getHeight());
});
}

@Override
protected void onDraw(android.graphics.Canvas canvas) {
if (graphicsMode != 2) return; // Fix for Android Pie
CanvasWrapper g = canvasWrapper;
g.bind(canvas);
g.clear(backgroundColor);
offscreenCopy.getBitmap().prepareToDraw();
g.drawImage(offscreenCopy, virtualScreen);
if (fpsCounter != null) {
fpsCounter.increment();
}
}
}
}
Loading

0 comments on commit 18c0293

Please sign in to comment.