Skip to content

Commit

Permalink
(squashed)
Browse files Browse the repository at this point in the history
more testing

sometimes, it's the little details that matter most

bleep blorp

blorp

bleep

trying to get view binding

testing...

black screen

debugging

not quite
  • Loading branch information
jakedowns committed Jan 22, 2024
1 parent 3fb4d5c commit fe05ed2
Show file tree
Hide file tree
Showing 12 changed files with 149 additions and 27 deletions.
2 changes: 0 additions & 2 deletions src/android/app/proguard-rules.pro
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,6 @@
-dontwarn java.beans.VetoableChangeListener
-dontwarn java.beans.VetoableChangeSupport

-keep class org.citra.citra_emu.vendor.simongellis.leia.webxr.** { *; }

-keep class com.leia.sdk.** { *; }
-keep class com.leia.core.** { *; }
-keep class com.leia.internal.** { *; }
Expand Down
77 changes: 77 additions & 0 deletions src/android/app/src/main/java/org/citra/citra_emu/LeiaHelper3D.kt
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
package org.citra.citra_emu

import android.app.Application
import android.content.Context
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.util.Log
import android.view.Surface
import android.widget.Toast
import com.leia.core.LogLevel
import com.leia.sdk.LeiaSDK
import com.leia.sdk.views.InputViewsAsset
import com.leia.sdk.views.InterlacedSurfaceView
import org.citra.citra_emu.vendor.simongellis.leia.webxr.LeiaTextureRenderer
import org.citra.citra_emu.vendor.simongellis.leia.webxr.RendererImpl

class LeiaSurfaceView(context: Context, attrs: AttributeSet) : InterlacedSurfaceView(context, attrs) {
private val textureRenderer = LeiaTextureRenderer()
private val asset = InputViewsAsset(RendererImpl(textureRenderer))

// constructor(context: Context?) : this(context, null)
// constructor(context: Context?, attrs: AttributeSet?) : super(context, attrs)

fun init() {
Log.i("LeiaHelper3D: LeiaSurfaceView","init")
setViewAsset(asset)
}

fun setSurfaceListener(surfaceListener: LeiaHelper3D.SurfaceListener) {
val surfaceAsset = InputViewsAsset.createEmptySurfaceForVideo {
surfaceTexture: SurfaceTexture? ->
Log.d("SurfaceListener", "createEmptySurfaceForVideo -> calling onSurfaceChanged")
surfaceListener.onSurfaceChanged(Surface(surfaceTexture))
}
setViewAsset(surfaceAsset)
}

fun addTexture(texture: SurfaceTexture, transform: FloatArray) {
Log.d("SurfaceListener", "addTexture")
textureRenderer.addTexture(texture, transform)
}
}

class LeiaHelper3D {

fun interface SurfaceListener {
fun onSurfaceChanged(surface: Surface)
}

companion object {
fun init(application: Application) {
try {
val initArgs = LeiaSDK.InitArgs().apply {
platform.app = application
platform.logLevel = LogLevel.Trace
}
val leiaSDK = LeiaSDK.createSDK(initArgs)
leiaSDK.startFaceTracking(false)
} catch (e: Exception) {
Log.e("MainApp", "Failed to initialize LeiaSDK: ${e.message}")
Toast.makeText(application, "Failed to initialize LeiaSDK", Toast.LENGTH_SHORT).show()
}
}

fun update3dMode(surfaceView: InterlacedSurfaceView, enable3dMode: Boolean, hasFocus: Boolean) {
surfaceView.config.use { config ->
config.setNumTiles(if (enable3dMode) 2 else 1, 1)
}

val leiaSDK = LeiaSDK.getInstance()
leiaSDK?.let {
it.startFaceTracking(enable3dMode && hasFocus)
it.enableBacklight(enable3dMode && hasFocus)
}
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ import androidx.core.view.WindowInsetsControllerCompat
import androidx.navigation.fragment.NavHostFragment
import androidx.preference.PreferenceManager
import org.citra.citra_emu.CitraApplication
import org.citra.citra_emu.LeiaHelper3D
import org.citra.citra_emu.NativeLibrary
import org.citra.citra_emu.R
import org.citra.citra_emu.camera.StillImageCameraHelper.OnFilePickerResult
Expand Down Expand Up @@ -60,6 +61,8 @@ class EmulationActivity : AppCompatActivity() {
override fun onCreate(savedInstanceState: Bundle?) {
ThemeUtil.setTheme(this)

LeiaHelper3D.init(application)

settingsViewModel.settings.loadSettings()

super.onCreate(savedInstanceState)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -8,10 +8,7 @@ import android.annotation.SuppressLint
import android.content.Context
import android.content.DialogInterface
import android.content.SharedPreferences
import android.graphics.SurfaceTexture
import android.net.Uri
import android.opengl.GLES20
import android.opengl.Matrix
import android.os.Bundle
import android.os.Handler
import android.os.Looper
Expand Down Expand Up @@ -62,15 +59,18 @@ import org.citra.citra_emu.features.settings.utils.SettingsFile
import org.citra.citra_emu.model.Game
import org.citra.citra_emu.utils.DirectoryInitialization
import org.citra.citra_emu.utils.DirectoryInitialization.DirectoryInitializationState
import org.citra.citra_emu.utils.EmulationLifecycleUtil
import org.citra.citra_emu.utils.EmulationMenuSettings
import org.citra.citra_emu.utils.FileUtil
import org.citra.citra_emu.utils.GameHelper
import org.citra.citra_emu.utils.GameIconUtils
import org.citra.citra_emu.utils.EmulationLifecycleUtil
import org.citra.citra_emu.utils.Log
import org.citra.citra_emu.utils.ViewUtils
import org.citra.citra_emu.vendor.simongellis.leia.webxr.LeiaSurfaceView
import org.citra.citra_emu.viewmodel.EmulationViewModel
// -- leia --
import org.citra.citra_emu.LeiaHelper3D
import org.citra.citra_emu.LeiaSurfaceView;


class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.FrameCallback {
private val preferences: SharedPreferences
Expand All @@ -80,6 +80,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
private var perfStatsUpdater: Runnable? = null

private lateinit var emulationActivity: EmulationActivity
private lateinit var leiaSurfaceView: LeiaSurfaceView

private var _binding: FragmentEmulationBinding? = null
private val binding get() = _binding!!
Expand Down Expand Up @@ -167,7 +168,21 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
if (requireActivity().isFinishing) {
return
}
binding.surfaceEmulation.holder.addCallback(this)



val sL = LeiaHelper3D.SurfaceListener{ surface ->
Log.debug("SurfaceListener onSurfaceChanged called") // Log debug warning
emulationState.newSurface(surface)
}
binding.surfaceEmulation.setSurfaceListener(sL)

/*try{
binding.surfaceEmulation.holder.addCallback(this);
}catch(e: Exception){
Log.debug("SurfaceListener error: $e") // Log debug warning
}*/

binding.doneControlConfig.setOnClickListener {
binding.doneControlConfig.visibility = View.GONE
binding.surfaceInputOverlay.setIsInEditMode(false)
Expand Down Expand Up @@ -228,6 +243,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
binding.inGameMenu.setNavigationItemSelectedListener {
when (it.itemId) {
R.id.menu_emulation_pause -> {
LeiaHelper3D.update3dMode(binding.surfaceEmulation, !emulationState.isPaused, !emulationState.isPaused)
if (emulationState.isPaused) {
emulationState.unpause()
it.title = resources.getString(R.string.pause_emulation)
Expand Down Expand Up @@ -343,6 +359,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
return
}

LeiaHelper3D.update3dMode(binding.surfaceEmulation, false, false)

if (binding.drawerLayout.isOpen) {
binding.drawerLayout.close()
} else {
Expand Down Expand Up @@ -425,6 +443,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram

override fun onResume() {
super.onResume()
LeiaHelper3D.update3dMode(binding.surfaceEmulation, true, true)
Choreographer.getInstance().postFrameCallback(this)
if (NativeLibrary.isRunning()) {
NativeLibrary.unPauseEmulation()
Expand All @@ -439,6 +458,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram
}

override fun onPause() {
LeiaHelper3D.update3dMode(binding.surfaceEmulation, false, false)
if (NativeLibrary.isRunning()) {
emulationState.pause()
}
Expand Down Expand Up @@ -860,17 +880,16 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback, Choreographer.Fram

override fun surfaceCreated(holder: SurfaceHolder) {
Log.debug("[EmulationFragment] Surface Created")
// We purposely don't do anything here.
// All work is done in surfaceChanged, which we are guaranteed to get even for surface creation.
}

override fun surfaceChanged(holder: SurfaceHolder, format: Int, width: Int, height: Int) {
Log.debug("[EmulationFragment] Surface changed. Resolution: " + width + "x" + height)
emulationState.newSurface(binding.surfaceEmulation.holder.surface)
emulationState.newSurface(holder.surface)
}

override fun surfaceDestroyed(holder: SurfaceHolder) {
emulationState.clearSurface()
LeiaHelper3D.update3dMode(binding.surfaceEmulation, false, false)
}

override fun doFrame(frameTimeNanos: Long) {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -3,14 +3,16 @@ package org.citra.citra_emu.vendor.simongellis.leia.webxr
import android.content.Context
import android.graphics.SurfaceTexture
import android.util.AttributeSet
import android.util.Log
import com.leia.sdk.views.InputViewsAsset
import com.leia.sdk.views.InterlacedSurfaceView

open class LeiaSurfaceView(context: Context, attrs: AttributeSet) : InterlacedSurfaceView(context, attrs) {
class LeiaSurfaceView(context: Context, attrs: AttributeSet) : InterlacedSurfaceView(context, attrs) {
private val textureRenderer = LeiaTextureRenderer()
private val asset = InputViewsAsset(RendererImpl(textureRenderer))

init {
Log.i("LeiaSurfaceView", "init")
setViewAsset(asset)
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -23,10 +23,12 @@ class LeiaTextureRenderer : Renderer {
private var texLocation = -1

fun addTexture(texture: SurfaceTexture, transform: FloatArray) {
Log.d(TAG, "addTexture")
textureHolders.add(TextureHolder(texture, transform))
}

override fun onSurfaceCreated() {
Log.d(TAG, "onSurfaceCreated")
val textureIds = IntArray(textureHolders.size)
glGenTextures(textureIds.size, textureIds, 0)
textureHolders.forEachIndexed { index, textureHolder ->
Expand All @@ -48,10 +50,12 @@ class LeiaTextureRenderer : Renderer {
}

override fun onSurfaceChanged(width: Int, height: Int) {
Log.d(TAG, "onSurfaceChanged")
size = Size(width, height)
}

override fun onDrawFrame() {
Log.d(TAG, "onDrawFrame")
glViewport(0, 0, size.width, size.height)
logError("glViewport")
glUseProgram(program)
Expand All @@ -69,6 +73,7 @@ class LeiaTextureRenderer : Renderer {
}

private fun renderTexture(holder: TextureHolder) {
Log.d(TAG, "renderTexture")
holder.tryUpdateTexImage()
val textureId = holder.textureId
val mv = holder.transform
Expand Down Expand Up @@ -149,7 +154,8 @@ class LeiaTextureRenderer : Renderer {
void main() {
gl_Position = u_MV * a_Pos;
// leia renders upside down, so flip Y values
v_TexCoord = vec2(a_TexCoord.x, 1.0f - a_TexCoord.y);
// citra ALSO renders upside down, so we good... (otherwise do: ..., 1.0 - a_TexCoord.y)
v_TexCoord = vec2(a_TexCoord.x, a_TexCoord.y);
}
"""

Expand Down
6 changes: 3 additions & 3 deletions src/android/app/src/main/jni/default_ini.h
Original file line number Diff line number Diff line change
Expand Up @@ -155,12 +155,12 @@ bg_blue =
bg_green =
# Whether and how Stereoscopic 3D should be rendered
# 0 (default): Off, 1: Side by Side, 2: Anaglyph, 3: Interlaced, 4: Reverse Interlaced, 5: Cardboard VR
render_3d =
# 0 (default): Off, 1: Side by Side, 2: Anaglyph, 3: Interlaced, 4: Reverse Interlaced, 5: Cardboard VR, 6: Lumepad 1, 7: Lumepad 2
render_3d = 7
# Change 3D Intensity
# 0 - 100: Intensity. 0 (default)
factor_3d =
factor_3d = 50
# The name of the post processing shader to apply.
# Loaded from shaders if render_3d is off or side by side.
Expand Down
6 changes: 5 additions & 1 deletion src/android/app/src/main/jni/emu_window/emu_window.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -83,7 +83,11 @@ EmuWindow_Android::~EmuWindow_Android() {
}

void EmuWindow_Android::MakeCurrent() {
core_context->MakeCurrent();
try {
core_context->MakeCurrent();
} catch (const std::exception& e) {
LOG_DEBUG(Frontend, "Exception caught in MakeCurrent: {}", e.what());
}
}

void EmuWindow_Android::DoneCurrent() {
Expand Down
24 changes: 16 additions & 8 deletions src/android/app/src/main/jni/emu_window/emu_window_gl.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -75,11 +75,11 @@ class SharedContext_Android : public Frontend::GraphicsContext {
EmuWindow_Android_OpenGL::EmuWindow_Android_OpenGL(Core::System& system_, ANativeWindow* surface)
: EmuWindow_Android{surface}, system{system_} {
if (egl_display = eglGetDisplay(EGL_DEFAULT_DISPLAY); egl_display == EGL_NO_DISPLAY) {
LOG_CRITICAL(Frontend, "eglGetDisplay() failed");
LOG_CRITICAL(Frontend, "eglGetDisplay() failed 1");
return;
}
if (eglInitialize(egl_display, 0, 0) != EGL_TRUE) {
LOG_CRITICAL(Frontend, "eglInitialize() failed");
LOG_CRITICAL(Frontend, "eglInitialize() failed 2");
return;
}
if (EGLint egl_num_configs{}; eglChooseConfig(egl_display, egl_attribs.data(), &egl_config, 1,
Expand All @@ -99,12 +99,12 @@ EmuWindow_Android_OpenGL::EmuWindow_Android_OpenGL(Core::System& system_, ANativ

if (egl_context = eglCreateContext(egl_display, egl_config, 0, egl_context_attribs.data());
egl_context == EGL_NO_CONTEXT) {
LOG_CRITICAL(Frontend, "eglCreateContext() failed");
LOG_CRITICAL(Frontend, "eglCreateContext() failed a");
return;
}
if (eglSurfaceAttrib(egl_display, egl_surface, EGL_SWAP_BEHAVIOR, EGL_BUFFER_DESTROYED) !=
EGL_TRUE) {
LOG_CRITICAL(Frontend, "eglSurfaceAttrib() failed");
LOG_CRITICAL(Frontend, "eglSurfaceAttrib() failed b");
return;
}
if (core_context = CreateSharedContext(); !core_context) {
Expand Down Expand Up @@ -136,12 +136,20 @@ bool EmuWindow_Android_OpenGL::CreateWindowSurface() {
eglGetConfigAttrib(egl_display, egl_config, EGL_NATIVE_VISUAL_ID, &format);
ANativeWindow_setBuffersGeometry(host_window, 0, 0, format);

if (egl_surface = eglCreateWindowSurface(egl_display, egl_config, host_window, 0);
egl_surface == EGL_NO_SURFACE) {
return {};
// Check if a surface already exists and destroy it
if (egl_surface != EGL_NO_SURFACE) {
eglDestroySurface(egl_display, egl_surface);
}

return egl_surface;
egl_surface = eglCreateWindowSurface(egl_display, egl_config, host_window, nullptr);

if (egl_surface == EGL_NO_SURFACE) {
EGLint error = eglGetError(); // Get the error code
LOG_CRITICAL(Frontend, "EmuWindow_Android_OpenGL eglCreateWindowSurface() returned error {}", error);
return true;
}

return true;
}

void EmuWindow_Android_OpenGL::DestroyWindowSurface() {
Expand Down
2 changes: 1 addition & 1 deletion src/android/app/src/main/res/layout/fragment_emulation.xml
Original file line number Diff line number Diff line change
Expand Up @@ -12,7 +12,7 @@
android:layout_height="match_parent">

<!-- This is what everything is rendered to during emulation -->
<org.citra.citra_emu.vendor.simongellis.leia.webxr.LeiaSurfaceView
<org.citra.citra_emu.LeiaSurfaceView
android:id="@+id/surface_emulation"
android:layout_width="match_parent"
android:layout_height="match_parent"
Expand Down
5 changes: 4 additions & 1 deletion src/android/app/src/main/res/values/arrays.xml
Original file line number Diff line number Diff line change
Expand Up @@ -141,6 +141,8 @@
<item>@string/interlaced</item>
<item>@string/reverse_interlaced</item>
<item>@string/cardboard_vr</item>
<item>@string/lumepad_1</item>
<item>@string/lumepad_2</item>
</string-array>

<integer-array name="render3dValues">
Expand All @@ -150,6 +152,8 @@
<item>3</item>
<item>4</item>
<item>5</item>
<item>6</item>
<item>7</item>
</integer-array>

<string-array name="graphicsApiNames">
Expand Down Expand Up @@ -450,5 +454,4 @@
<item>11</item>
<item>12</item>
</integer-array>

</resources>
Loading

0 comments on commit fe05ed2

Please sign in to comment.