1
0
mirror of https://github.com/godotengine/godot.git synced 2025-11-07 12:30:27 +00:00

Merge pull request #71875 from m4gr3d/editor_optimization_3x

[3.x] Add benchmark logic
This commit is contained in:
Fredia Huya-Kouadio
2023-04-22 20:18:45 -07:00
committed by GitHub
23 changed files with 460 additions and 26 deletions

View File

@@ -39,6 +39,7 @@ import org.godotengine.godot.io.file.FileAccessHandler;
import org.godotengine.godot.plugin.GodotPlugin;
import org.godotengine.godot.plugin.GodotPluginRegistry;
import org.godotengine.godot.tts.GodotTTS;
import org.godotengine.godot.utils.BenchmarkUtils;
import org.godotengine.godot.utils.GodotNetUtils;
import org.godotengine.godot.utils.PermissionsUtil;
import org.godotengine.godot.xr.XRMode;
@@ -268,6 +269,8 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
public GodotIO io;
public GodotNetUtils netUtils;
public GodotTTS tts;
private DirectoryAccessHandler directoryAccessHandler;
private FileAccessHandler fileAccessHandler;
static SingletonBase[] singletons = new SingletonBase[MAX_SINGLETONS];
static int singleton_count = 0;
@@ -601,7 +604,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
}
return cmdline;
} catch (Exception e) {
e.printStackTrace();
// The _cl_ file can be missing with no adverse effect
return new String[0];
}
}
@@ -662,8 +665,8 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
netUtils = new GodotNetUtils(activity);
tts = new GodotTTS(activity);
Context context = getContext();
DirectoryAccessHandler directoryAccessHandler = new DirectoryAccessHandler(context);
FileAccessHandler fileAccessHandler = new FileAccessHandler(context);
directoryAccessHandler = new DirectoryAccessHandler(context);
fileAccessHandler = new FileAccessHandler(context);
mSensorManager = (SensorManager)activity.getSystemService(Context.SENSOR_SERVICE);
mAccelerometer = mSensorManager.getDefaultSensor(Sensor.TYPE_ACCELEROMETER);
mGravity = mSensorManager.getDefaultSensor(Sensor.TYPE_GRAVITY);
@@ -685,6 +688,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
@Override
public void onCreate(Bundle icicle) {
BenchmarkUtils.beginBenchmarkMeasure("Godot::onCreate");
super.onCreate(icicle);
final Activity activity = getActivity();
@@ -736,6 +740,18 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
editor.putString("store_public_key", main_pack_key);
editor.apply();
i++;
} else if (command_line[i].equals("--benchmark")) {
BenchmarkUtils.setUseBenchmark(true);
new_args.add(command_line[i]);
} else if (has_extra && command_line[i].equals("--benchmark-file")) {
BenchmarkUtils.setUseBenchmark(true);
new_args.add(command_line[i]);
// Retrieve the filepath
BenchmarkUtils.setBenchmarkFile(command_line[i + 1]);
new_args.add(command_line[i + 1]);
i++;
} else if (command_line[i].trim().length() != 0) {
new_args.add(command_line[i]);
@@ -807,6 +823,7 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
mCurrentIntent = activity.getIntent();
initializeGodot();
BenchmarkUtils.endBenchmarkMeasure("Godot::onCreate");
}
@Override
@@ -1021,22 +1038,6 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
// Do something here if sensor accuracy changes.
}
/*
@Override public boolean dispatchKeyEvent(KeyEvent event) {
if (event.getKeyCode()==KeyEvent.KEYCODE_BACK) {
System.out.printf("** BACK REQUEST!\n");
GodotLib.quit();
return true;
}
System.out.printf("** OTHER KEY!\n");
return false;
}
*/
public void onBackPressed() {
boolean shouldQuit = true;
@@ -1242,6 +1243,16 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
mView.initInputDevices();
}
@Keep
public DirectoryAccessHandler getDirectoryAccessHandler() {
return directoryAccessHandler;
}
@Keep
public FileAccessHandler getFileAccessHandler() {
return fileAccessHandler;
}
@Keep
private int createNewGodotInstance(String[] args) {
if (godotHost != null) {
@@ -1249,4 +1260,19 @@ public class Godot extends Fragment implements SensorEventListener, IDownloaderC
}
return 0;
}
@Keep
private void beginBenchmarkMeasure(String label) {
BenchmarkUtils.beginBenchmarkMeasure(label);
}
@Keep
private void endBenchmarkMeasure(String label) {
BenchmarkUtils.endBenchmarkMeasure(label);
}
@Keep
private void dumpBenchmark(String benchmarkFile) {
BenchmarkUtils.dumpBenchmark(fileAccessHandler, benchmarkFile);
}
}

View File

@@ -43,14 +43,21 @@ import org.godotengine.godot.xr.regular.RegularFallbackConfigChooser;
import android.annotation.SuppressLint;
import android.content.Context;
import android.content.res.AssetManager;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.PixelFormat;
import android.os.Build;
import android.text.TextUtils;
import android.util.SparseArray;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.PointerIcon;
import androidx.annotation.Keep;
import java.io.InputStream;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
@@ -79,6 +86,7 @@ public class GodotView extends GLSurfaceView {
private final Godot godot;
private final GodotInputHandler inputHandler;
private final GodotRenderer godotRenderer;
private final SparseArray<PointerIcon> customPointerIcons = new SparseArray<>();
private EGLConfigChooser eglConfigChooser;
private EGLContextFactory eglContextFactory;
@@ -149,13 +157,48 @@ public class GodotView extends GLSurfaceView {
inputHandler.onPointerCaptureChange(false);
}
/**
* Used to configure the PointerIcon for the given type.
*
* Called from JNI
*/
@Keep
public void configurePointerIcon(int pointerType, String imagePath, float hotSpotX, float hotSpotY) {
if (android.os.Build.VERSION.SDK_INT >= android.os.Build.VERSION_CODES.N) {
try {
Bitmap bitmap = null;
if (!TextUtils.isEmpty(imagePath)) {
if (godot.getDirectoryAccessHandler().filesystemFileExists(imagePath)) {
// Try to load the bitmap from the file system
bitmap = BitmapFactory.decodeFile(imagePath);
} else if (godot.getDirectoryAccessHandler().assetsFileExists(imagePath)) {
// Try to load the bitmap from the assets directory
AssetManager am = getContext().getAssets();
InputStream imageInputStream = am.open(imagePath);
bitmap = BitmapFactory.decodeStream(imageInputStream);
}
}
PointerIcon customPointerIcon = PointerIcon.create(bitmap, hotSpotX, hotSpotY);
customPointerIcons.put(pointerType, customPointerIcon);
} catch (Exception e) {
// Reset the custom pointer icon
customPointerIcons.delete(pointerType);
}
}
}
/**
* Called from JNI to change the pointer icon
*/
@Keep
private void setPointerIcon(int pointerType) {
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.N) {
setPointerIcon(PointerIcon.getSystemIcon(getContext(), pointerType));
PointerIcon pointerIcon = customPointerIcons.get(pointerType);
if (pointerIcon == null) {
pointerIcon = PointerIcon.getSystemIcon(getContext(), pointerType);
}
setPointerIcon(pointerIcon);
}
}

View File

@@ -79,6 +79,9 @@ class DirectoryAccessHandler(context: Context) {
private val assetsDirAccess = AssetsDirectoryAccess(context)
private val fileSystemDirAccess = FilesystemDirectoryAccess(context)
fun assetsFileExists(assetsPath: String) = assetsDirAccess.fileExists(assetsPath)
fun filesystemFileExists(path: String) = fileSystemDirAccess.fileExists(path)
private fun hasDirId(accessType: AccessType, dirId: Int): Boolean {
return when (accessType) {
ACCESS_RESOURCES -> assetsDirAccess.hasDirId(dirId)

View File

@@ -46,7 +46,7 @@ class FileAccessHandler(val context: Context) {
private val TAG = FileAccessHandler::class.java.simpleName
private const val FILE_NOT_FOUND_ERROR_ID = -1
private const val INVALID_FILE_ID = 0
internal const val INVALID_FILE_ID = 0
private const val STARTING_FILE_ID = 1
internal fun fileExists(context: Context, storageScopeIdentifier: StorageScope.Identifier, path: String?): Boolean {
@@ -96,13 +96,17 @@ class FileAccessHandler(val context: Context) {
private fun hasFileId(fileId: Int) = files.indexOfKey(fileId) >= 0
fun fileOpen(path: String?, modeFlags: Int): Int {
val accessFlag = FileAccessFlags.fromNativeModeFlags(modeFlags) ?: return INVALID_FILE_ID
return fileOpen(path, accessFlag)
}
internal fun fileOpen(path: String?, accessFlag: FileAccessFlags): Int {
val storageScope = storageScopeIdentifier.identifyStorageScope(path)
if (storageScope == StorageScope.UNKNOWN) {
return INVALID_FILE_ID
}
try {
val accessFlag = FileAccessFlags.fromNativeModeFlags(modeFlags) ?: return INVALID_FILE_ID
val dataAccess = DataAccess.generateDataAccess(storageScope, context, path!!, accessFlag) ?: return INVALID_FILE_ID
files.put(++lastFileId, dataAccess)

View File

@@ -0,0 +1,122 @@
/**************************************************************************/
/* BenchmarkUtils.kt */
/**************************************************************************/
/* This file is part of: */
/* GODOT ENGINE */
/* https://godotengine.org */
/**************************************************************************/
/* Copyright (c) 2014-present Godot Engine contributors (see AUTHORS.md). */
/* Copyright (c) 2007-2014 Juan Linietsky, Ariel Manzur. */
/* */
/* Permission is hereby granted, free of charge, to any person obtaining */
/* a copy of this software and associated documentation files (the */
/* "Software"), to deal in the Software without restriction, including */
/* without limitation the rights to use, copy, modify, merge, publish, */
/* distribute, sublicense, and/or sell copies of the Software, and to */
/* permit persons to whom the Software is furnished to do so, subject to */
/* the following conditions: */
/* */
/* The above copyright notice and this permission notice shall be */
/* included in all copies or substantial portions of the Software. */
/* */
/* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, */
/* EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF */
/* MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. */
/* IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY */
/* CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, */
/* TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE */
/* SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE. */
/**************************************************************************/
@file:JvmName("BenchmarkUtils")
package org.godotengine.godot.utils
import android.os.Build
import android.os.SystemClock
import android.os.Trace
import android.util.Log
import org.godotengine.godot.BuildConfig
import org.godotengine.godot.io.file.FileAccessFlags
import org.godotengine.godot.io.file.FileAccessHandler
import org.json.JSONObject
import java.nio.ByteBuffer
import java.util.concurrent.ConcurrentSkipListMap
/**
* Contains benchmark related utilities methods
*/
private const val TAG = "GodotBenchmark"
var useBenchmark = false
var benchmarkFile = ""
private val startBenchmarkFrom = ConcurrentSkipListMap<String, Long>()
private val benchmarkTracker = ConcurrentSkipListMap<String, Double>()
/**
* Start measuring and tracing the execution of a given section of code using the given label.
*
* Must be followed by a call to [endBenchmarkMeasure].
*
* Note: Only enabled on 'editorDev' build variant.
*/
fun beginBenchmarkMeasure(label: String) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
return
}
startBenchmarkFrom[label] = SystemClock.elapsedRealtime()
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Trace.beginAsyncSection(label, 0)
}
}
/**
* End measuring and tracing of the section of code with the given label.
*
* Must be preceded by a call [beginBenchmarkMeasure]
*
* Note: Only enabled on 'editorDev' build variant.
*/
fun endBenchmarkMeasure(label: String) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
return
}
val startTime = startBenchmarkFrom[label] ?: return
val total = SystemClock.elapsedRealtime() - startTime
benchmarkTracker[label] = total / 1000.0
if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.Q) {
Trace.endAsyncSection(label, 0)
}
}
/**
* Dump the benchmark measurements.
* If [filepath] is valid, the data is also written in json format to the specified file.
*
* Note: Only enabled on 'editorDev' build variant.
*/
@JvmOverloads
fun dumpBenchmark(fileAccessHandler: FileAccessHandler?, filepath: String? = benchmarkFile) {
if (BuildConfig.FLAVOR != "editor" || BuildConfig.BUILD_TYPE != "dev") {
return
}
if (!useBenchmark) {
return
}
val printOut =
benchmarkTracker.map { "\t- ${it.key} : ${it.value} sec." }.joinToString("\n")
Log.i(TAG, "BENCHMARK:\n$printOut")
if (fileAccessHandler != null && !filepath.isNullOrBlank()) {
val fileId = fileAccessHandler.fileOpen(filepath, FileAccessFlags.WRITE)
if (fileId != FileAccessHandler.INVALID_FILE_ID) {
val jsonOutput = JSONObject(benchmarkTracker.toMap()).toString(4)
fileAccessHandler.fileWrite(fileId, ByteBuffer.wrap(jsonOutput.toByteArray()))
fileAccessHandler.fileClose(fileId)
}
}
}