This commit is contained in:
2026-01-02 15:33:26 +01:00
commit 655c38ef45
93 changed files with 7348 additions and 0 deletions

BIN
Assets/Plugins/Android/cpp/.DS_Store vendored Normal file

Binary file not shown.

View File

@@ -0,0 +1,30 @@
cmake_minimum_required(VERSION 3.22.1)
project("MyNativePlugin")
find_library(log-lib log)
set(CMAKE_CXX_STANDARD 23)
set(CMAKE_CXX_STANDARD_REQUIRED ON)
set(PLUGIN_API "/Applications/Unity/Hub/Editor/6000.3.2f1/Unity.app/Contents/PluginAPI")
set(ANDROID_SDK "/Users/omar/Library/Android/sdk")
set(BINDER_DIR "${ANDROID_SDK}/platforms/android-36/optional/libbinder_ndk_cpp")
set(SHARED_SRC_DIR "/Users/omar/Desktop/NativeService/src/main/cpp/")
add_library(my_native_lib SHARED
my_native_code.cpp
${SHARED_SRC_DIR}/com/omixlab/mosis/IMosisService.cpp
${SHARED_SRC_DIR}/com/omixlab/mosis/IMosisListener.cpp
${SHARED_SRC_DIR}/logger.cpp
${SHARED_SRC_DIR}/external_texture.cpp
${SHARED_SRC_DIR}/render_target.cpp
${SHARED_SRC_DIR}/glad/src/egl.c
${SHARED_SRC_DIR}/glad/src/gles2.c
)
target_link_libraries(my_native_lib ${log-lib} binder_ndk EGL GLESv2 nativewindow)
target_include_directories(my_native_lib PUBLIC
${SHARED_SRC_DIR}
${SHARED_SRC_DIR}/glad/include
${BINDER_DIR}
${PLUGIN_API}
)

View File

@@ -0,0 +1,7 @@
fileFormatVersion: 2
guid: 321e7452e1c8c4c74812b77bd3f296d5
TextScriptImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,8 @@
fileFormatVersion: 2
guid: 604c411489f9043dcbd071e443f5dc07
folderAsset: yes
DefaultImporter:
externalObjects: {}
userData:
assetBundleName:
assetBundleVariant:

View File

@@ -0,0 +1,93 @@
#if UNITY_EDITOR
using UnityEditor;
using UnityEditor.Android;
using System.IO;
using UnityEngine;
using System;
using System.Xml;
public class AndroidPostProcess : IPostGenerateGradleAndroidProject
{
public int callbackOrder => 0;
public void OnPostGenerateGradleAndroidProject(string buildPath)
{
// 1. Get the path to the Unity-generated CMakeLists.txt in the build folder
// path parameter is: PROJECT_ROOT/Library/Bee/Android/Prj/Mono2x/Gradle/unityLibrary
string targetCmakeFile = Path.Combine(buildPath, "src/main/cpp/CMakeLists.txt");
if (!File.Exists(targetCmakeFile)) return;
// 2. Locate your custom C++ folder in Assets
// Application.dataPath gives the absolute path to "Assets"
string myCppFolder = Path.GetFullPath(Path.Combine(Application.dataPath, "Plugins/Android/cpp"));
// 3. Calculate the relative path from the build's CMake location to your source
// We need the directory where the target CMake file lives to calculate relative from it
string targetCmakeDir = Path.GetDirectoryName(targetCmakeFile);
string relativePathToMyCpp = GetRelativePath(targetCmakeDir, myCppFolder).Replace("\\", "/");
// 4. Prepare the command
string commandName = "my_plugin_build";
string lineToAdd = $"\nadd_subdirectory(\"{relativePathToMyCpp}\" \"{commandName}\")\n";
// 5. Prevent multiple additions
string currentContent = File.ReadAllText(targetCmakeFile);
if (!currentContent.Contains(relativePathToMyCpp))
{
File.AppendAllText(targetCmakeFile, lineToAdd);
Debug.Log($"[CMake] Added {relativePathToMyCpp} to build.");
}
ModifyManifest(buildPath);
}
// Helper for cross-platform relative paths
private string GetRelativePath(string fromPath, string toPath)
{
Uri fromUri = new Uri(AppendSlash(fromPath));
Uri toUri = new Uri(AppendSlash(toPath));
Uri relativeUri = fromUri.MakeRelativeUri(toUri);
return Uri.UnescapeDataString(relativeUri.ToString());
}
private string AppendSlash(string path)
{
if (!path.EndsWith(Path.DirectorySeparatorChar.ToString()))
path += Path.DirectorySeparatorChar;
return path;
}
public void ModifyManifest(string path)
{
// The manifest is located in the unityLibrary module
string manifestPath = Path.Combine(path, "src/main/AndroidManifest.xml");
XmlDocument doc = new XmlDocument();
doc.Load(manifestPath);
XmlNode root = doc.DocumentElement;
XmlNamespaceManager nsmgr = new XmlNamespaceManager(doc.NameTable);
nsmgr.AddNamespace("android", "http://schemas.android.com/apk/res/android");
// 1. Check/Add Queries tag
XmlNode queriesNode = root.SelectSingleNode("queries");
if (queriesNode == null)
{
queriesNode = doc.CreateElement("queries");
root.AppendChild(queriesNode);
}
// 2. Add the specific package if it's not there
string targetPackage = "com.omixlab.mosis";
if (queriesNode.SelectSingleNode($"package[@android:name='{targetPackage}']", nsmgr) == null)
{
XmlElement packageElem = doc.CreateElement("package");
packageElem.SetAttribute("name", "http://schemas.android.com/apk/res/android", targetPackage);
queriesNode.AppendChild(packageElem);
}
doc.Save(manifestPath);
Debug.Log("[Manifest] Successfully injected <queries> tag.");
}
}
#endif

View File

@@ -0,0 +1,2 @@
fileFormatVersion: 2
guid: 813722810ec374244ad7f0fbd8b40b7e

View File

@@ -0,0 +1,195 @@
#include <jni.h>
#include <aidl/com/omixlab/mosis/IMosisListener.h>
#include <aidl/com/omixlab/mosis/BnMosisListener.h>
#include <logger.h>
#include <aidl/com/omixlab/mosis/IMosisService.h>
#include <android/binder_ibinder_jni.h>
#include <format>
#include <glad/gles2.h>
#include <glad/egl.h>
#include <IUnityGraphics.h>
#include <external_texture.h>
#include <render_target.h>
using namespace aidl::com::omixlab::mosis;
using namespace aidl::android::hardware;
std::shared_ptr<class IMosisService> g_service;
std::shared_ptr<class ServiceContext> g_context;
class TextureBlitter
{
GLuint source_texture = 0;
GLuint source_fb = 0;
GLuint dest_texture = 0;
GLuint dest_fb = 0;
GLint width;
GLint height;
public:
bool create(AHardwareBuffer* hardwareBuffer)
{
AHardwareBuffer_Desc desc{};
AHardwareBuffer_describe(hardwareBuffer, &desc);
width = desc.width;
height = desc.height;
EGLClientBuffer clientBuffer = eglGetNativeClientBufferANDROID(hardwareBuffer);
EGLImageKHR eglImage = eglCreateImageKHR(eglGetCurrentDisplay(), EGL_NO_CONTEXT,
EGL_NATIVE_BUFFER_ANDROID, clientBuffer, nullptr);
if (eglImage == EGL_NO_IMAGE_KHR)
{
Logger::Log("Failed to create EGL image");
return false;
}
glGenTextures(1, &source_texture);
glBindTexture(GL_TEXTURE_EXTERNAL_OES, source_texture);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_MAG_FILTER, GL_NEAREST);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_EXTERNAL_OES, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glEGLImageTargetTexture2DOES(GL_TEXTURE_EXTERNAL_OES, eglImage);
eglDestroyImageKHR(eglGetCurrentDisplay(), eglImage);
glGenFramebuffers(1, &source_fb);
glBindFramebuffer(GL_FRAMEBUFFER, source_fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_EXTERNAL_OES, source_texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
glGenTextures(1, &dest_texture);
glBindTexture(GL_TEXTURE_2D, dest_texture);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE);
glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE);
glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB, desc.width, desc.height,
0, GL_RGB, GL_UNSIGNED_BYTE, nullptr);
glGenFramebuffers(1, &dest_fb);
glBindFramebuffer(GL_FRAMEBUFFER, dest_fb);
glFramebufferTexture2D(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
GL_TEXTURE_2D, dest_texture, 0);
if (glCheckFramebufferStatus(GL_FRAMEBUFFER) != GL_FRAMEBUFFER_COMPLETE)
return false;
blit();
return true;
}
[[nodiscard]] GLuint dest_texture_id() const
{
return dest_texture;
}
void blit()
{
glBindFramebuffer(GL_READ_FRAMEBUFFER, source_fb);
glBindFramebuffer(GL_DRAW_FRAMEBUFFER, dest_fb);
glBlitFramebuffer(0, 0, width, height, 0, 0, width, height, GL_COLOR_BUFFER_BIT, GL_NEAREST);
}
};
typedef void (*OnMessageCallback)(const char*);
typedef void (*OnServiceInitializedCallback)(bool success);
typedef void (*OnFrameAvailableCallback)();
typedef void (*OnBufferReadyCallback)();
typedef void (*OnTextureReadyCallback)(GLuint gl_texture);
struct NativeCallbacks
{
OnMessageCallback OnMessage;
OnServiceInitializedCallback OnServiceInitialized;
OnFrameAvailableCallback OnFrameAvailable;
OnBufferReadyCallback OnBufferReady;
OnTextureReadyCallback OnTextureReady;
};
NativeCallbacks g_callbacks{};
class ServiceContext : public BnMosisListener
{
AHardwareBuffer* m_hwbuffer = nullptr;
std::unique_ptr<TextureBlitter> m_texture;
public:
ndk::ScopedAStatus onServiceInitialized(bool in_success) override
{
Logger::Log("onServiceInitialized");
g_callbacks.OnMessage("onServiceInitialized");
g_callbacks.OnServiceInitialized(in_success);
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus onFrameAvailable() override
{
Logger::Log("onFrameAvailable");
g_callbacks.OnMessage("onFrameAvailable");
g_callbacks.OnFrameAvailable();
return ndk::ScopedAStatus::ok();
}
ndk::ScopedAStatus onBufferAvailable(const HardwareBuffer &in_buffer) override
{
Logger::Log("onBufferAvailable");
g_callbacks.OnMessage("onBufferAvailable");
m_hwbuffer = in_buffer.get();
AHardwareBuffer_acquire(m_hwbuffer);
g_callbacks.OnBufferReady();
return ndk::ScopedAStatus::ok();
}
[[nodiscard]] GLuint create_texture()
{
m_texture = std::make_unique<TextureBlitter>();
m_texture->create(m_hwbuffer);
return m_texture->dest_texture_id();
}
void update_texture()
{
if (m_texture)
m_texture->blit();
}
};
extern "C" void SetNativeCallbacks(const NativeCallbacks& ptr)
{
g_callbacks = ptr;
}
extern "C" UnityRenderingEvent InitGLAD()
{
return [](int eventId){
gladLoaderLoadEGL(EGL_NO_DISPLAY);
int egl_version = gladLoadEGL(eglGetCurrentDisplay(), eglGetProcAddress);
if (egl_version == 0)
{
Logger::Log("Failed to load EGL");
return;
}
int gl_version = gladLoaderLoadGLES2();
if (gl_version == 0)
{
Logger::Log("Failed to load GL");
return;
}
if (g_context)
{
GLuint texture = g_context->create_texture();
g_callbacks.OnTextureReady(texture);
}
};
}
extern "C" UnityRenderingEvent UpdateTexture()
{
return [](int eventId){
if (g_context)
{
g_context->update_texture();
}
};
}
extern "C"
JNIEXPORT void JNICALL
Java_com_omixlab_mosis_unity_MyKotlinPlugin_serviceConnected(JNIEnv *env, jobject thiz,
jobject binder)
{
AIBinder* pBinder = AIBinder_fromJavaBinder(env, binder);
const ndk::SpAIBinder spBinder(pBinder);
g_service = IMosisService::fromBinder(spBinder);
Logger::Log("Service Connected");
g_context = ndk::SharedRefBase::make<ServiceContext>();
bool result{};
g_service->initOS(g_context, &result);
Logger::Log(std::format("InitOS returned {}", result));
}

View File

@@ -0,0 +1,54 @@
fileFormatVersion: 2
guid: 2d802269589194242b4f22af14a9bc73
PluginImporter:
externalObjects: {}
serializedVersion: 3
iconMap: {}
executionOrder: {}
defineConstraints: []
isPreloaded: 0
isOverridable: 0
isExplicitlyReferenced: 0
validateReferences: 1
platformData:
Android:
enabled: 0
settings:
AndroidLibraryDependee: UnityLibrary
AndroidSharedLibraryType: Executable
CPU: ARMv7
Any:
enabled: 0
settings:
Exclude Android: 1
Exclude Editor: 1
Exclude Linux64: 1
Exclude OSXUniversal: 1
Exclude WebGL: 1
Exclude Win: 1
Exclude Win64: 1
Editor:
enabled: 0
settings:
CPU: AnyCPU
DefaultValueInitialized: true
OS: AnyOS
Linux64:
enabled: 0
settings:
CPU: x86_64
OSXUniversal:
enabled: 0
settings:
CPU: None
Win:
enabled: 0
settings:
CPU: x86
Win64:
enabled: 0
settings:
CPU: None
userData:
assetBundleName:
assetBundleVariant: