From e9d2e7a7ac76acf65538d1ef87d25944aa9bd477 Mon Sep 17 00:00:00 2001 From: pineappleEA Date: Thu, 31 Aug 2023 00:14:34 +0200 Subject: [PATCH] early-access version 3849 --- README.md | 2 +- .../java/org/yuzu/yuzu_emu/NativeLibrary.kt | 22 +++- .../yuzu_emu/activities/EmulationActivity.kt | 15 +++ .../org/yuzu/yuzu_emu/adapters/GameAdapter.kt | 24 +--- .../DiskShaderCacheProgress.kt | 50 ++++---- .../yuzu_emu/fragments/EmulationFragment.kt | 110 +++++++++++++----- .../yuzu/yuzu_emu/model/EmulationViewModel.kt | 59 ++++++++++ .../org/yuzu/yuzu_emu/utils/GameIconUtils.kt | 77 ++++++++++++ src/android/app/src/main/jni/id_cache.cpp | 14 +++ src/android/app/src/main/jni/id_cache.h | 2 + src/android/app/src/main/jni/native.cpp | 22 +++- .../main/res/layout/fragment_emulation.xml | 83 ++++++++++++- .../app/src/main/res/values-de/strings.xml | 1 - .../app/src/main/res/values-es/strings.xml | 1 - .../app/src/main/res/values-fr/strings.xml | 1 - .../app/src/main/res/values-it/strings.xml | 1 - .../app/src/main/res/values-ja/strings.xml | 1 - .../app/src/main/res/values-ko/strings.xml | 1 - .../app/src/main/res/values-nb/strings.xml | 1 - .../app/src/main/res/values-pl/strings.xml | 1 - .../src/main/res/values-pt-rBR/strings.xml | 1 - .../src/main/res/values-pt-rPT/strings.xml | 1 - .../app/src/main/res/values-ru/strings.xml | 1 - .../app/src/main/res/values-uk/strings.xml | 1 - .../src/main/res/values-zh-rCN/strings.xml | 1 - .../src/main/res/values-zh-rTW/strings.xml | 1 - .../app/src/main/res/values/strings.xml | 2 +- 27 files changed, 393 insertions(+), 103 deletions(-) create mode 100755 src/android/app/src/main/java/org/yuzu/yuzu_emu/model/EmulationViewModel.kt create mode 100755 src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt diff --git a/README.md b/README.md index 9c5f62966..2ee786023 100755 --- a/README.md +++ b/README.md @@ -1,7 +1,7 @@ yuzu emulator early access ============= -This is the source code for early-access 3848. +This is the source code for early-access 3849. ## Legal Notice diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt index 5a7cf4ed7..c8706d7a6 100755 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/NativeLibrary.kt @@ -22,9 +22,7 @@ import org.yuzu.yuzu_emu.utils.FileUtil.exists import org.yuzu.yuzu_emu.utils.FileUtil.getFileSize import org.yuzu.yuzu_emu.utils.FileUtil.isDirectory import org.yuzu.yuzu_emu.utils.FileUtil.openContentUri -import org.yuzu.yuzu_emu.utils.Log.error -import org.yuzu.yuzu_emu.utils.Log.verbose -import org.yuzu.yuzu_emu.utils.Log.warning +import org.yuzu.yuzu_emu.utils.Log import org.yuzu.yuzu_emu.utils.SerializableHelper.serializable /** @@ -465,7 +463,7 @@ object NativeLibrary { val emulationActivity = sEmulationActivity.get() if (emulationActivity == null) { - warning("[NativeLibrary] EmulationActivity is null, can't exit.") + Log.warning("[NativeLibrary] EmulationActivity is null, can't exit.") return } @@ -490,15 +488,27 @@ object NativeLibrary { } fun setEmulationActivity(emulationActivity: EmulationActivity?) { - verbose("[NativeLibrary] Registering EmulationActivity.") + Log.verbose("[NativeLibrary] Registering EmulationActivity.") sEmulationActivity = WeakReference(emulationActivity) } fun clearEmulationActivity() { - verbose("[NativeLibrary] Unregistering EmulationActivity.") + Log.verbose("[NativeLibrary] Unregistering EmulationActivity.") sEmulationActivity.clear() } + @Keep + @JvmStatic + fun onEmulationStarted() { + sEmulationActivity.get()!!.onEmulationStarted() + } + + @Keep + @JvmStatic + fun onEmulationStopped(status: Int) { + sEmulationActivity.get()!!.onEmulationStopped(status) + } + /** * Logs the Yuzu version, Android version and, CPU. */ diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt index dbd602a1d..bbd328c71 100755 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/activities/EmulationActivity.kt @@ -28,6 +28,7 @@ import android.view.Surface import android.view.View import android.view.inputmethod.InputMethodManager import android.widget.Toast +import androidx.activity.viewModels import androidx.appcompat.app.AppCompatActivity import androidx.core.view.WindowCompat import androidx.core.view.WindowInsetsCompat @@ -41,6 +42,7 @@ import org.yuzu.yuzu_emu.databinding.ActivityEmulationBinding import org.yuzu.yuzu_emu.features.settings.model.BooleanSetting import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.features.settings.model.Settings +import org.yuzu.yuzu_emu.model.EmulationViewModel import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.utils.ControllerMappingHelper import org.yuzu.yuzu_emu.utils.ForegroundService @@ -70,8 +72,11 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { private val actionMute = "ACTION_EMULATOR_MUTE" private val actionUnmute = "ACTION_EMULATOR_UNMUTE" + private val emulationViewModel: EmulationViewModel by viewModels() + override fun onDestroy() { stopForegroundService(this) + emulationViewModel.clear() super.onDestroy() } @@ -416,6 +421,16 @@ class EmulationActivity : AppCompatActivity(), SensorEventListener { } } + fun onEmulationStarted() { + emulationViewModel.setEmulationStarted(true) + } + + fun onEmulationStopped(status: Int) { + if (status == 0) { + finish() + } + } + private fun startMotionSensorListener() { val sensorManager = this.getSystemService(Context.SENSOR_SERVICE) as SensorManager val gyroSensor = sensorManager.getDefaultSensor(Sensor.TYPE_GYROSCOPE) diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt index e91277d35..13359ef36 100755 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/adapters/GameAdapter.kt @@ -3,8 +3,6 @@ package org.yuzu.yuzu_emu.adapters -import android.graphics.Bitmap -import android.graphics.BitmapFactory import android.net.Uri import android.text.TextUtils import android.view.LayoutInflater @@ -15,23 +13,20 @@ import android.widget.Toast import androidx.appcompat.app.AppCompatActivity import androidx.documentfile.provider.DocumentFile import androidx.lifecycle.ViewModelProvider -import androidx.lifecycle.lifecycleScope import androidx.navigation.findNavController import androidx.preference.PreferenceManager import androidx.recyclerview.widget.AsyncDifferConfig import androidx.recyclerview.widget.DiffUtil import androidx.recyclerview.widget.ListAdapter import androidx.recyclerview.widget.RecyclerView -import coil.load -import kotlinx.coroutines.launch import org.yuzu.yuzu_emu.HomeNavigationDirections -import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R import org.yuzu.yuzu_emu.YuzuApplication import org.yuzu.yuzu_emu.adapters.GameAdapter.GameViewHolder import org.yuzu.yuzu_emu.databinding.CardGameBinding import org.yuzu.yuzu_emu.model.Game import org.yuzu.yuzu_emu.model.GamesViewModel +import org.yuzu.yuzu_emu.utils.GameIconUtils class GameAdapter(private val activity: AppCompatActivity) : ListAdapter(AsyncDifferConfig.Builder(DiffCallback()).build()), @@ -98,12 +93,7 @@ class GameAdapter(private val activity: AppCompatActivity) : this.game = game binding.imageGameScreen.scaleType = ImageView.ScaleType.CENTER_CROP - activity.lifecycleScope.launch { - val bitmap = decodeGameIcon(game.path) - binding.imageGameScreen.load(bitmap) { - error(R.drawable.default_icon) - } - } + GameIconUtils.loadGameIcon(game, binding.imageGameScreen) binding.textGameTitle.text = game.title.replace("[\\t\\n\\r]+".toRegex(), " ") @@ -126,14 +116,4 @@ class GameAdapter(private val activity: AppCompatActivity) : return oldItem == newItem } } - - private fun decodeGameIcon(uri: String): Bitmap? { - val data = NativeLibrary.getIcon(uri) - return BitmapFactory.decodeByteArray( - data, - 0, - data.size, - BitmapFactory.Options() - ) - } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress.kt index a18efef19..6f4b5b13f 100755 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/disk_shader_cache/DiskShaderCacheProgress.kt @@ -4,43 +4,43 @@ package org.yuzu.yuzu_emu.disk_shader_cache import androidx.annotation.Keep +import androidx.lifecycle.ViewModelProvider import org.yuzu.yuzu_emu.NativeLibrary import org.yuzu.yuzu_emu.R -import org.yuzu.yuzu_emu.disk_shader_cache.ui.ShaderProgressDialogFragment +import org.yuzu.yuzu_emu.activities.EmulationActivity +import org.yuzu.yuzu_emu.model.EmulationViewModel +import org.yuzu.yuzu_emu.utils.Log @Keep object DiskShaderCacheProgress { - val finishLock = Object() - private lateinit var fragment: ShaderProgressDialogFragment + private lateinit var emulationViewModel: EmulationViewModel - private fun prepareDialog() { - val emulationActivity = NativeLibrary.sEmulationActivity.get()!! - emulationActivity.runOnUiThread { - fragment = ShaderProgressDialogFragment.newInstance( - emulationActivity.getString(R.string.loading), - emulationActivity.getString(R.string.preparing_shaders) - ) - fragment.show( - emulationActivity.supportFragmentManager, - ShaderProgressDialogFragment.TAG - ) - } - synchronized(finishLock) { finishLock.wait() } + private fun prepareViewModel() { + emulationViewModel = + ViewModelProvider( + NativeLibrary.sEmulationActivity.get() as EmulationActivity + )[EmulationViewModel::class.java] } @JvmStatic fun loadProgress(stage: Int, progress: Int, max: Int) { val emulationActivity = NativeLibrary.sEmulationActivity.get() - ?: error("[DiskShaderCacheProgress] EmulationActivity not present") + if (emulationActivity == null) { + Log.error("[DiskShaderCacheProgress] EmulationActivity not present") + return + } - when (LoadCallbackStage.values()[stage]) { - LoadCallbackStage.Prepare -> prepareDialog() - LoadCallbackStage.Build -> fragment.onUpdateProgress( - emulationActivity.getString(R.string.building_shaders), - progress, - max - ) - LoadCallbackStage.Complete -> fragment.dismiss() + emulationActivity.runOnUiThread { + when (LoadCallbackStage.values()[stage]) { + LoadCallbackStage.Prepare -> prepareViewModel() + LoadCallbackStage.Build -> emulationViewModel.updateProgress( + emulationActivity.getString(R.string.building_shaders), + progress, + max + ) + + LoadCallbackStage.Complete -> {} + } } } diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt index 53f19c4f8..944ae652e 100755 --- a/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/fragments/EmulationFragment.kt @@ -24,8 +24,9 @@ import androidx.core.content.res.ResourcesCompat import androidx.core.graphics.Insets import androidx.core.view.ViewCompat import androidx.core.view.WindowInsetsCompat -import androidx.core.view.isVisible +import androidx.drawerlayout.widget.DrawerLayout import androidx.fragment.app.Fragment +import androidx.fragment.app.activityViewModels import androidx.lifecycle.Lifecycle import androidx.lifecycle.lifecycleScope import androidx.lifecycle.repeatOnLifecycle @@ -50,6 +51,7 @@ import org.yuzu.yuzu_emu.features.settings.model.IntSetting import org.yuzu.yuzu_emu.features.settings.model.Settings import org.yuzu.yuzu_emu.features.settings.utils.SettingsFile import org.yuzu.yuzu_emu.model.Game +import org.yuzu.yuzu_emu.model.EmulationViewModel import org.yuzu.yuzu_emu.overlay.InputOverlay import org.yuzu.yuzu_emu.utils.* @@ -66,6 +68,8 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { private lateinit var game: Game + private val emulationViewModel: EmulationViewModel by activityViewModels() + private var isInFoldableLayout = false override fun onAttach(context: Context) { @@ -130,9 +134,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { binding.showFpsText.setTextColor(Color.YELLOW) binding.doneControlConfig.setOnClickListener { stopConfiguringControls() } - // Setup overlay. - updateShowFpsOverlay() - + binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) binding.inGameMenu.getHeaderView(0).findViewById(R.id.text_game_title).text = game.title binding.inGameMenu.setNavigationItemSelectedListener { @@ -174,7 +176,9 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { R.id.menu_exit -> { emulationState.stop() - requireActivity().finish() + emulationViewModel.setIsEmulationStopping(true) + binding.drawerLayout.close() + binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_LOCKED_CLOSED) true } @@ -188,6 +192,10 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { requireActivity(), object : OnBackPressedCallback(true) { override fun handleOnBackPressed() { + if (!NativeLibrary.isRunning()) { + return + } + if (binding.drawerLayout.isOpen) { binding.drawerLayout.close() } else { @@ -204,6 +212,54 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { .collect { updateFoldableLayout(requireActivity() as EmulationActivity, it) } } } + + GameIconUtils.loadGameIcon(game, binding.loadingImage) + binding.loadingTitle.text = game.title + binding.loadingTitle.isSelected = true + binding.loadingText.isSelected = true + + emulationViewModel.shaderProgress.observe(viewLifecycleOwner) { + if (it > 0 && it != emulationViewModel.totalShaders.value!!) { + binding.loadingProgressIndicator.isIndeterminate = false + + if (it < binding.loadingProgressIndicator.max) { + binding.loadingProgressIndicator.progress = it + } + } + + if (it == emulationViewModel.totalShaders.value!!) { + binding.loadingText.setText(R.string.loading) + binding.loadingProgressIndicator.isIndeterminate = true + } + } + emulationViewModel.totalShaders.observe(viewLifecycleOwner) { + binding.loadingProgressIndicator.max = it + } + emulationViewModel.shaderMessage.observe(viewLifecycleOwner) { + if (it.isNotEmpty()) { + binding.loadingText.text = it + } + } + + emulationViewModel.emulationStarted.observe(viewLifecycleOwner) { started -> + if (started) { + binding.drawerLayout.setDrawerLockMode(DrawerLayout.LOCK_MODE_UNLOCKED) + ViewUtils.showView(binding.surfaceInputOverlay) + ViewUtils.hideView(binding.loadingIndicator) + + // Setup overlay + updateShowFpsOverlay() + } + } + + emulationViewModel.isEmulationStopping.observe(viewLifecycleOwner) { + if (it) { + binding.loadingText.setText(R.string.shutting_down) + ViewUtils.showView(binding.loadingIndicator) + ViewUtils.hideView(binding.inputContainer) + ViewUtils.hideView(binding.showFpsText) + } + } } override fun onConfigurationChanged(newConfig: Configuration) { @@ -213,11 +269,21 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { binding.drawerLayout.close() } if (EmulationMenuSettings.showOverlay) { - binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = false } + binding.surfaceInputOverlay.post { + binding.surfaceInputOverlay.visibility = View.VISIBLE + } } } else { - if (EmulationMenuSettings.showOverlay) { - binding.surfaceInputOverlay.post { binding.surfaceInputOverlay.isVisible = true } + if (EmulationMenuSettings.showOverlay && + emulationViewModel.emulationStarted.value == true + ) { + binding.surfaceInputOverlay.post { + binding.surfaceInputOverlay.visibility = View.VISIBLE + } + } else { + binding.surfaceInputOverlay.post { + binding.surfaceInputOverlay.visibility = View.INVISIBLE + } } if (!isInFoldableLayout) { if (newConfig.orientation == Configuration.ORIENTATION_PORTRAIT) { @@ -226,9 +292,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { binding.surfaceInputOverlay.layout = InputOverlay.LANDSCAPE } } - if (!binding.surfaceInputOverlay.isInEditMode) { - refreshInputOverlay() - } } } @@ -260,10 +323,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { super.onDetach() } - private fun refreshInputOverlay() { - binding.surfaceInputOverlay.refreshControls() - } - private fun resetInputOverlay() { preferences.edit() .remove(Settings.PREF_CONTROL_SCALE) @@ -281,17 +340,15 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { val FRAMETIME = 2 val SPEED = 3 perfStatsUpdater = { - val perfStats = NativeLibrary.getPerfStats() - if (perfStats[FPS] > 0 && _binding != null) { - binding.showFpsText.text = String.format("FPS: %.1f", perfStats[FPS]) - } - - if (!emulationState.isStopped) { + if (emulationViewModel.emulationStarted.value == true) { + val perfStats = NativeLibrary.getPerfStats() + if (perfStats[FPS] > 0 && _binding != null) { + binding.showFpsText.text = String.format("FPS: %.1f", perfStats[FPS]) + } perfStatsUpdateHandler.postDelayed(perfStatsUpdater!!, 100) } } perfStatsUpdateHandler.post(perfStatsUpdater!!) - binding.showFpsText.text = resources.getString(R.string.emulation_game_loading) binding.showFpsText.visibility = View.VISIBLE } else { if (perfStatsUpdater != null) { @@ -349,7 +406,6 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { isInFoldableLayout = true binding.surfaceInputOverlay.layout = InputOverlay.FOLDABLE - refreshInputOverlay() } } it.isSeparating @@ -437,7 +493,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { .apply() } .setPositiveButton(android.R.string.ok) { _, _ -> - refreshInputOverlay() + binding.surfaceInputOverlay.refreshControls() } .setNegativeButton(android.R.string.cancel, null) .setNeutralButton(R.string.emulation_toggle_all) { _, _ -> } @@ -461,7 +517,7 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { R.id.menu_show_overlay -> { it.isChecked = !it.isChecked EmulationMenuSettings.showOverlay = it.isChecked - refreshInputOverlay() + binding.surfaceInputOverlay.refreshControls() true } @@ -567,14 +623,14 @@ class EmulationFragment : Fragment(), SurfaceHolder.Callback { preferences.edit() .putInt(Settings.PREF_CONTROL_SCALE, scale) .apply() - refreshInputOverlay() + binding.surfaceInputOverlay.refreshControls() } private fun setControlOpacity(opacity: Int) { preferences.edit() .putInt(Settings.PREF_CONTROL_OPACITY, opacity) .apply() - refreshInputOverlay() + binding.surfaceInputOverlay.refreshControls() } private fun setInsets() { diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/EmulationViewModel.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/EmulationViewModel.kt new file mode 100755 index 000000000..e35f51bc3 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/model/EmulationViewModel.kt @@ -0,0 +1,59 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-3.0-or-later + +package org.yuzu.yuzu_emu.model + +import androidx.lifecycle.LiveData +import androidx.lifecycle.MutableLiveData +import androidx.lifecycle.ViewModel + +class EmulationViewModel : ViewModel() { + private val _emulationStarted = MutableLiveData(false) + val emulationStarted: LiveData get() = _emulationStarted + + private val _isEmulationStopping = MutableLiveData(false) + val isEmulationStopping: LiveData get() = _isEmulationStopping + + private val _shaderProgress = MutableLiveData(0) + val shaderProgress: LiveData get() = _shaderProgress + + private val _totalShaders = MutableLiveData(0) + val totalShaders: LiveData get() = _totalShaders + + private val _shaderMessage = MutableLiveData("") + val shaderMessage: LiveData get() = _shaderMessage + + fun setEmulationStarted(started: Boolean) { + _emulationStarted.postValue(started) + } + + fun setIsEmulationStopping(value: Boolean) { + _isEmulationStopping.value = value + } + + fun setShaderProgress(progress: Int) { + _shaderProgress.value = progress + } + + fun setTotalShaders(max: Int) { + _totalShaders.value = max + } + + fun setShaderMessage(msg: String) { + _shaderMessage.value = msg + } + + fun updateProgress(msg: String, progress: Int, max: Int) { + setShaderMessage(msg) + setShaderProgress(progress) + setTotalShaders(max) + } + + fun clear() { + _emulationStarted.value = false + _isEmulationStopping.value = false + _shaderProgress.value = 0 + _totalShaders.value = 0 + _shaderMessage.value = "" + } +} diff --git a/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt new file mode 100755 index 000000000..c0fe596d7 --- /dev/null +++ b/src/android/app/src/main/java/org/yuzu/yuzu_emu/utils/GameIconUtils.kt @@ -0,0 +1,77 @@ +// SPDX-FileCopyrightText: 2023 yuzu Emulator Project +// SPDX-License-Identifier: GPL-2.0-or-later + +package org.yuzu.yuzu_emu.utils + +import android.graphics.Bitmap +import android.graphics.BitmapFactory +import android.widget.ImageView +import androidx.core.graphics.drawable.toDrawable +import coil.ImageLoader +import coil.decode.DataSource +import coil.fetch.DrawableResult +import coil.fetch.FetchResult +import coil.fetch.Fetcher +import coil.key.Keyer +import coil.memory.MemoryCache +import coil.request.ImageRequest +import coil.request.Options +import org.yuzu.yuzu_emu.NativeLibrary +import org.yuzu.yuzu_emu.R +import org.yuzu.yuzu_emu.YuzuApplication +import org.yuzu.yuzu_emu.model.Game + +class GameIconFetcher( + private val game: Game, + private val options: Options +) : Fetcher { + override suspend fun fetch(): FetchResult { + return DrawableResult( + drawable = decodeGameIcon(game.path)!!.toDrawable(options.context.resources), + isSampled = false, + dataSource = DataSource.DISK + ) + } + + private fun decodeGameIcon(uri: String): Bitmap? { + val data = NativeLibrary.getIcon(uri) + return BitmapFactory.decodeByteArray( + data, + 0, + data.size, + BitmapFactory.Options() + ) + } + + class Factory : Fetcher.Factory { + override fun create(data: Game, options: Options, imageLoader: ImageLoader): Fetcher = + GameIconFetcher(data, options) + } +} + +class GameIconKeyer : Keyer { + override fun key(data: Game, options: Options): String = data.path +} + +object GameIconUtils { + private val imageLoader = ImageLoader.Builder(YuzuApplication.appContext) + .components { + add(GameIconKeyer()) + add(GameIconFetcher.Factory()) + } + .memoryCache { + MemoryCache.Builder(YuzuApplication.appContext) + .maxSizePercent(0.25) + .build() + } + .build() + + fun loadGameIcon(game: Game, imageView: ImageView) { + val request = ImageRequest.Builder(YuzuApplication.appContext) + .data(game) + .target(imageView) + .error(R.drawable.default_icon) + .build() + imageLoader.enqueue(request) + } +} diff --git a/src/android/app/src/main/jni/id_cache.cpp b/src/android/app/src/main/jni/id_cache.cpp index 9cbbf23a3..960abf95a 100755 --- a/src/android/app/src/main/jni/id_cache.cpp +++ b/src/android/app/src/main/jni/id_cache.cpp @@ -15,6 +15,8 @@ static jclass s_disk_cache_progress_class; static jclass s_load_callback_stage_class; static jmethodID s_exit_emulation_activity; static jmethodID s_disk_cache_load_progress; +static jmethodID s_on_emulation_started; +static jmethodID s_on_emulation_stopped; static constexpr jint JNI_VERSION = JNI_VERSION_1_6; @@ -59,6 +61,14 @@ jmethodID GetDiskCacheLoadProgress() { return s_disk_cache_load_progress; } +jmethodID GetOnEmulationStarted() { + return s_on_emulation_started; +} + +jmethodID GetOnEmulationStopped() { + return s_on_emulation_stopped; +} + } // namespace IDCache #ifdef __cplusplus @@ -85,6 +95,10 @@ jint JNI_OnLoad(JavaVM* vm, void* reserved) { env->GetStaticMethodID(s_native_library_class, "exitEmulationActivity", "(I)V"); s_disk_cache_load_progress = env->GetStaticMethodID(s_disk_cache_progress_class, "loadProgress", "(III)V"); + s_on_emulation_started = + env->GetStaticMethodID(s_native_library_class, "onEmulationStarted", "()V"); + s_on_emulation_stopped = + env->GetStaticMethodID(s_native_library_class, "onEmulationStopped", "(I)V"); // Initialize Android Storage Common::FS::Android::RegisterCallbacks(env, s_native_library_class); diff --git a/src/android/app/src/main/jni/id_cache.h b/src/android/app/src/main/jni/id_cache.h index be535fe1e..b76158928 100755 --- a/src/android/app/src/main/jni/id_cache.h +++ b/src/android/app/src/main/jni/id_cache.h @@ -15,5 +15,7 @@ jclass GetDiskCacheProgressClass(); jclass GetDiskCacheLoadCallbackStageClass(); jmethodID GetExitEmulationActivity(); jmethodID GetDiskCacheLoadProgress(); +jmethodID GetOnEmulationStarted(); +jmethodID GetOnEmulationStopped(); } // namespace IDCache diff --git a/src/android/app/src/main/jni/native.cpp b/src/android/app/src/main/jni/native.cpp index b2adfdeda..0f2a6d9e4 100755 --- a/src/android/app/src/main/jni/native.cpp +++ b/src/android/app/src/main/jni/native.cpp @@ -203,12 +203,10 @@ public: } bool IsRunning() const { - std::scoped_lock lock(m_mutex); return m_is_running; } bool IsPaused() const { - std::scoped_lock lock(m_mutex); return m_is_running && m_is_paused; } @@ -335,6 +333,8 @@ public: // Tear down the render window. m_window.reset(); + + OnEmulationStopped(m_load_result); } void PauseEmulation() { @@ -376,6 +376,8 @@ public: m_system.InitializeDebugger(); } + OnEmulationStarted(); + while (true) { { [[maybe_unused]] std::unique_lock lock(m_mutex); @@ -511,6 +513,18 @@ private: static_cast(progress), static_cast(max)); } + static void OnEmulationStarted() { + JNIEnv* env = IDCache::GetEnvForThread(); + env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), + IDCache::GetOnEmulationStarted()); + } + + static void OnEmulationStopped(Core::SystemResultStatus result) { + JNIEnv* env = IDCache::GetEnvForThread(); + env->CallStaticVoidMethod(IDCache::GetNativeLibraryClass(), + IDCache::GetOnEmulationStopped(), static_cast(result)); + } + private: static EmulationSession s_instance; @@ -528,8 +542,8 @@ private: Core::PerfStatsResults m_perf_stats{}; std::shared_ptr m_vfs; Core::SystemResultStatus m_load_result{Core::SystemResultStatus::ErrorNotInitialized}; - bool m_is_running{}; - bool m_is_paused{}; + std::atomic m_is_running = false; + std::atomic m_is_paused = false; SoftwareKeyboard::AndroidKeyboard* m_software_keyboard{}; std::unique_ptr m_profile_manager; std::unique_ptr m_manual_provider; diff --git a/src/android/app/src/main/res/layout/fragment_emulation.xml b/src/android/app/src/main/res/layout/fragment_emulation.xml index e54a10e8f..da97d85c1 100755 --- a/src/android/app/src/main/res/layout/fragment_emulation.xml +++ b/src/android/app/src/main/res/layout/fragment_emulation.xml @@ -26,6 +26,81 @@ android:focusable="false" android:focusableInTouchMode="false" /> + + + + + + + + + + + + + + + + + + + + + android:focusableInTouchMode="true" + android:visibility="invisible" />