update external headers
This commit is contained in:
120
ios/include/common/CallbackUtils.cpp
Normal file
120
ios/include/common/CallbackUtils.cpp
Normal file
@@ -0,0 +1,120 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "CallbackUtils.h"
|
||||
|
||||
#include "private/backend/VirtualMachineEnv.h"
|
||||
|
||||
void acquireCallbackJni(JNIEnv* env, CallbackJni& callbackUtils) {
|
||||
#ifdef __ANDROID__
|
||||
callbackUtils.handlerClass = env->FindClass("android/os/Handler");
|
||||
callbackUtils.handlerClass = (jclass) env->NewGlobalRef(callbackUtils.handlerClass);
|
||||
callbackUtils.post = env->GetMethodID(callbackUtils.handlerClass,
|
||||
"post", "(Ljava/lang/Runnable;)Z");
|
||||
#endif
|
||||
|
||||
callbackUtils.executorClass = env->FindClass("java/util/concurrent/Executor");
|
||||
callbackUtils.executorClass = (jclass) env->NewGlobalRef(callbackUtils.executorClass);
|
||||
callbackUtils.execute = env->GetMethodID(callbackUtils.executorClass,
|
||||
"execute", "(Ljava/lang/Runnable;)V");
|
||||
}
|
||||
|
||||
void releaseCallbackJni(JNIEnv* env, CallbackJni callbackUtils, jobject handler, jobject callback) {
|
||||
if (handler && callback) {
|
||||
#ifdef __ANDROID__
|
||||
if (env->IsInstanceOf(handler, callbackUtils.handlerClass)) {
|
||||
env->CallBooleanMethod(handler, callbackUtils.post, callback);
|
||||
}
|
||||
#endif
|
||||
if (env->IsInstanceOf(handler, callbackUtils.executorClass)) {
|
||||
env->CallVoidMethod(handler, callbackUtils.execute, callback);
|
||||
}
|
||||
}
|
||||
env->DeleteGlobalRef(handler);
|
||||
env->DeleteGlobalRef(callback);
|
||||
#ifdef __ANDROID__
|
||||
env->DeleteGlobalRef(callbackUtils.handlerClass);
|
||||
#endif
|
||||
env->DeleteGlobalRef(callbackUtils.executorClass);
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
JniCallback* JniCallback::make(JNIEnv* env, jobject handler, jobject callback) {
|
||||
return new JniCallback(env, handler, callback);
|
||||
}
|
||||
|
||||
JniCallback::JniCallback(JNIEnv* env, jobject handler, jobject callback)
|
||||
: mHandler(env->NewGlobalRef(handler)),
|
||||
mCallback(env->NewGlobalRef(callback)) {
|
||||
acquireCallbackJni(env, mCallbackUtils);
|
||||
}
|
||||
|
||||
JniCallback::~JniCallback() = default;
|
||||
|
||||
void JniCallback::post(void* user, filament::backend::CallbackHandler::Callback callback) {
|
||||
callback(user);
|
||||
}
|
||||
|
||||
void JniCallback::postToJavaAndDestroy(JniCallback* callback) {
|
||||
JNIEnv* env = filament::VirtualMachineEnv::get().getEnvironment();
|
||||
releaseCallbackJni(env, callback->mCallbackUtils, callback->mHandler, callback->mCallback);
|
||||
delete callback;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
JniBufferCallback* JniBufferCallback::make(filament::Engine*,
|
||||
JNIEnv* env, jobject handler, jobject callback, AutoBuffer&& buffer) {
|
||||
return new JniBufferCallback(env, handler, callback, std::move(buffer));
|
||||
}
|
||||
|
||||
JniBufferCallback::JniBufferCallback(JNIEnv* env, jobject handler, jobject callback,
|
||||
AutoBuffer&& buffer)
|
||||
: JniCallback(env, handler, callback),
|
||||
mBuffer(std::move(buffer)) {
|
||||
}
|
||||
|
||||
JniBufferCallback::~JniBufferCallback() = default;
|
||||
|
||||
void JniBufferCallback::postToJavaAndDestroy(void*, size_t, void* user) {
|
||||
JniBufferCallback* callback = (JniBufferCallback*)user;
|
||||
JNIEnv* env = filament::VirtualMachineEnv::get().getEnvironment();
|
||||
callback->mBuffer.attachToJniThread(env);
|
||||
releaseCallbackJni(env, callback->mCallbackUtils, callback->mHandler, callback->mCallback);
|
||||
delete callback;
|
||||
}
|
||||
|
||||
// -----------------------------------------------------------------------------------------------
|
||||
|
||||
JniImageCallback* JniImageCallback::make(filament::Engine*,
|
||||
JNIEnv* env, jobject handler, jobject callback, long image) {
|
||||
return new JniImageCallback(env, handler, callback, image);
|
||||
}
|
||||
|
||||
JniImageCallback::JniImageCallback(JNIEnv* env, jobject handler, jobject callback, long image)
|
||||
: JniCallback(env, handler, callback),
|
||||
mImage(image) {
|
||||
}
|
||||
|
||||
JniImageCallback::~JniImageCallback() = default;
|
||||
|
||||
void JniImageCallback::postToJavaAndDestroy(void*, void* user) {
|
||||
JniImageCallback* callback = (JniImageCallback*)user;
|
||||
JNIEnv* env = filament::VirtualMachineEnv::get().getEnvironment();
|
||||
releaseCallbackJni(env, callback->mCallbackUtils, callback->mHandler, callback->mCallback);
|
||||
delete callback;
|
||||
}
|
||||
95
ios/include/common/CallbackUtils.h
Normal file
95
ios/include/common/CallbackUtils.h
Normal file
@@ -0,0 +1,95 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
#include "common/NioUtils.h"
|
||||
|
||||
#include <backend/CallbackHandler.h>
|
||||
|
||||
#include <filament/Engine.h>
|
||||
|
||||
struct CallbackJni {
|
||||
#ifdef __ANDROID__
|
||||
jclass handlerClass = nullptr;
|
||||
jmethodID post = nullptr;
|
||||
#endif
|
||||
jclass executorClass = nullptr;
|
||||
jmethodID execute = nullptr;
|
||||
};
|
||||
|
||||
void acquireCallbackJni(JNIEnv* env, CallbackJni& callbackUtils);
|
||||
void releaseCallbackJni(JNIEnv* env, CallbackJni callbackUtils, jobject handler, jobject callback);
|
||||
|
||||
struct JniCallback : private filament::backend::CallbackHandler {
|
||||
JniCallback(JniCallback const &) = delete;
|
||||
JniCallback(JniCallback&&) = delete;
|
||||
JniCallback& operator=(JniCallback const &) = delete;
|
||||
JniCallback& operator=(JniCallback&&) = delete;
|
||||
|
||||
// create a JniCallback
|
||||
static JniCallback* make(JNIEnv* env, jobject handler, jobject runnable);
|
||||
|
||||
// execute the callback on the java thread and destroy ourselves
|
||||
static void postToJavaAndDestroy(JniCallback* callback);
|
||||
|
||||
// CallbackHandler interface.
|
||||
void post(void* user, Callback callback) override;
|
||||
|
||||
// Get the CallbackHandler interface
|
||||
filament::backend::CallbackHandler* getHandler() noexcept { return this; }
|
||||
|
||||
jobject getCallbackObject() { return mCallback; }
|
||||
|
||||
protected:
|
||||
JniCallback(JNIEnv* env, jobject handler, jobject runnable);
|
||||
explicit JniCallback() = default; // this version does nothing
|
||||
virtual ~JniCallback();
|
||||
jobject mHandler{};
|
||||
jobject mCallback{};
|
||||
CallbackJni mCallbackUtils{};
|
||||
};
|
||||
|
||||
|
||||
struct JniBufferCallback : public JniCallback {
|
||||
// create a JniBufferCallback
|
||||
static JniBufferCallback* make(filament::Engine* engine,
|
||||
JNIEnv* env, jobject handler, jobject callback, AutoBuffer&& buffer);
|
||||
|
||||
// execute the callback on the java thread and destroy ourselves
|
||||
static void postToJavaAndDestroy(void*, size_t, void* user);
|
||||
|
||||
private:
|
||||
JniBufferCallback(JNIEnv* env, jobject handler, jobject callback, AutoBuffer&& buffer);
|
||||
virtual ~JniBufferCallback();
|
||||
AutoBuffer mBuffer;
|
||||
};
|
||||
|
||||
struct JniImageCallback : public JniCallback {
|
||||
// create a JniImageCallback
|
||||
static JniImageCallback* make(filament::Engine* engine, JNIEnv* env, jobject handler,
|
||||
jobject runnable, long image);
|
||||
|
||||
// execute the callback on the java thread and destroy ourselves
|
||||
static void postToJavaAndDestroy(void*, void* user);
|
||||
|
||||
private:
|
||||
JniImageCallback(JNIEnv* env, jobject handler, jobject runnable, long image);
|
||||
virtual ~JniImageCallback();
|
||||
long mImage;
|
||||
};
|
||||
151
ios/include/common/NioUtils.cpp
Normal file
151
ios/include/common/NioUtils.cpp
Normal file
@@ -0,0 +1,151 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#include "common/NioUtils.h"
|
||||
|
||||
#include <algorithm>
|
||||
|
||||
#include <utils/Log.h>
|
||||
|
||||
AutoBuffer::AutoBuffer(JNIEnv *env, jobject buffer, jint size, bool commit) noexcept :
|
||||
mEnv(env),
|
||||
mDoCommit(commit) {
|
||||
|
||||
mNioUtils.jniClass = env->FindClass("com/google/android/filament/NioUtils");
|
||||
mNioUtils.jniClass = (jclass) env->NewGlobalRef(mNioUtils.jniClass);
|
||||
|
||||
mNioUtils.getBasePointer = env->GetStaticMethodID(mNioUtils.jniClass,
|
||||
"getBasePointer", "(Ljava/nio/Buffer;JI)J");
|
||||
mNioUtils.getBaseArray = env->GetStaticMethodID(mNioUtils.jniClass,
|
||||
"getBaseArray", "(Ljava/nio/Buffer;)Ljava/lang/Object;");
|
||||
mNioUtils.getBaseArrayOffset = env->GetStaticMethodID(mNioUtils.jniClass,
|
||||
"getBaseArrayOffset", "(Ljava/nio/Buffer;I)I");
|
||||
mNioUtils.getBufferType = env->GetStaticMethodID(mNioUtils.jniClass,
|
||||
"getBufferType", "(Ljava/nio/Buffer;)I");
|
||||
|
||||
mBuffer = env->NewGlobalRef(buffer);
|
||||
|
||||
mType = (BufferType) env->CallStaticIntMethod(
|
||||
mNioUtils.jniClass, mNioUtils.getBufferType, mBuffer);
|
||||
|
||||
switch (mType) {
|
||||
case BufferType::BYTE:
|
||||
mShift = 0;
|
||||
break;
|
||||
case BufferType::CHAR:
|
||||
case BufferType::SHORT:
|
||||
mShift = 1;
|
||||
break;
|
||||
case BufferType::INT:
|
||||
case BufferType::FLOAT:
|
||||
mShift = 2;
|
||||
break;
|
||||
case BufferType::LONG:
|
||||
case BufferType::DOUBLE:
|
||||
mShift = 3;
|
||||
break;
|
||||
}
|
||||
|
||||
mSize = (size_t) size << mShift;
|
||||
|
||||
jlong address = (jlong) env->GetDirectBufferAddress(mBuffer);
|
||||
if (address) {
|
||||
// Direct buffer case
|
||||
mData = reinterpret_cast<void *>(env->CallStaticLongMethod(mNioUtils.jniClass,
|
||||
mNioUtils.getBasePointer, mBuffer, address, mShift));
|
||||
mUserData = mData;
|
||||
} else {
|
||||
// wrapped array case
|
||||
jarray array = (jarray) env->CallStaticObjectMethod(mNioUtils.jniClass,
|
||||
mNioUtils.getBaseArray, mBuffer);
|
||||
|
||||
jint offset = env->CallStaticIntMethod(mNioUtils.jniClass,
|
||||
mNioUtils.getBaseArrayOffset, mBuffer, mShift);
|
||||
|
||||
mBaseArray = (jarray) env->NewGlobalRef(array);
|
||||
switch (mType) {
|
||||
case BufferType::BYTE:
|
||||
mData = env->GetByteArrayElements((jbyteArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::CHAR:
|
||||
mData = env->GetCharArrayElements((jcharArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::SHORT:
|
||||
mData = env->GetShortArrayElements((jshortArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::INT:
|
||||
mData = env->GetIntArrayElements((jintArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::LONG:
|
||||
mData = env->GetLongArrayElements((jlongArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::FLOAT:
|
||||
mData = env->GetFloatArrayElements((jfloatArray)mBaseArray, nullptr);
|
||||
break;
|
||||
case BufferType::DOUBLE:
|
||||
mData = env->GetDoubleArrayElements((jdoubleArray)mBaseArray, nullptr);
|
||||
break;
|
||||
}
|
||||
mUserData = (void *) ((char *) mData + offset);
|
||||
}
|
||||
}
|
||||
|
||||
AutoBuffer::AutoBuffer(AutoBuffer &&rhs) noexcept {
|
||||
mEnv = rhs.mEnv;
|
||||
std::swap(mData, rhs.mData);
|
||||
std::swap(mUserData, rhs.mUserData);
|
||||
std::swap(mSize, rhs.mSize);
|
||||
std::swap(mType, rhs.mType);
|
||||
std::swap(mShift, rhs.mShift);
|
||||
std::swap(mBuffer, rhs.mBuffer);
|
||||
std::swap(mBaseArray, rhs.mBaseArray);
|
||||
std::swap(mNioUtils, rhs.mNioUtils);
|
||||
}
|
||||
|
||||
AutoBuffer::~AutoBuffer() noexcept {
|
||||
JNIEnv *env = mEnv;
|
||||
if (mBaseArray) {
|
||||
jint mode = mDoCommit ? 0 : JNI_ABORT;
|
||||
switch (mType) {
|
||||
case BufferType::BYTE:
|
||||
env->ReleaseByteArrayElements((jbyteArray)mBaseArray, (jbyte *) mData, mode);
|
||||
break;
|
||||
case BufferType::CHAR:
|
||||
env->ReleaseCharArrayElements((jcharArray)mBaseArray, (jchar *) mData, mode);
|
||||
break;
|
||||
case BufferType::SHORT:
|
||||
env->ReleaseShortArrayElements((jshortArray)mBaseArray, (jshort *) mData, mode);
|
||||
break;
|
||||
case BufferType::INT:
|
||||
env->ReleaseIntArrayElements((jintArray)mBaseArray, (jint *) mData, mode);
|
||||
break;
|
||||
case BufferType::LONG:
|
||||
env->ReleaseLongArrayElements((jlongArray)mBaseArray, (jlong *) mData, mode);
|
||||
break;
|
||||
case BufferType::FLOAT:
|
||||
env->ReleaseFloatArrayElements((jfloatArray)mBaseArray, (jfloat *) mData, mode);
|
||||
break;
|
||||
case BufferType::DOUBLE:
|
||||
env->ReleaseDoubleArrayElements((jdoubleArray)mBaseArray, (jdouble *) mData, mode);
|
||||
break;
|
||||
}
|
||||
env->DeleteGlobalRef(mBaseArray);
|
||||
}
|
||||
if (mBuffer) {
|
||||
env->DeleteGlobalRef(mBuffer);
|
||||
}
|
||||
mEnv->DeleteGlobalRef(mNioUtils.jniClass);
|
||||
}
|
||||
80
ios/include/common/NioUtils.h
Normal file
80
ios/include/common/NioUtils.h
Normal file
@@ -0,0 +1,80 @@
|
||||
/*
|
||||
* Copyright (C) 2017 The Android Open Source Project
|
||||
*
|
||||
* Licensed under the Apache License, Version 2.0 (the "License");
|
||||
* you may not use this file except in compliance with the License.
|
||||
* You may obtain a copy of the License at
|
||||
*
|
||||
* http://www.apache.org/licenses/LICENSE-2.0
|
||||
*
|
||||
* Unless required by applicable law or agreed to in writing, software
|
||||
* distributed under the License is distributed on an "AS IS" BASIS,
|
||||
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
||||
* See the License for the specific language governing permissions and
|
||||
* limitations under the License.
|
||||
*/
|
||||
|
||||
#pragma once
|
||||
|
||||
#include <cstdint>
|
||||
|
||||
#include <jni.h>
|
||||
|
||||
class AutoBuffer {
|
||||
public:
|
||||
enum class BufferType : uint8_t {
|
||||
BYTE,
|
||||
CHAR,
|
||||
SHORT,
|
||||
INT,
|
||||
LONG,
|
||||
FLOAT,
|
||||
DOUBLE
|
||||
};
|
||||
|
||||
// Clients should pass "true" for the commit argument if they intend to mutate the buffer
|
||||
// contents from native code.
|
||||
AutoBuffer(JNIEnv* env, jobject buffer, jint size, bool commit = false) noexcept;
|
||||
AutoBuffer(AutoBuffer&& rhs) noexcept;
|
||||
~AutoBuffer() noexcept;
|
||||
|
||||
void attachToJniThread(JNIEnv* env) noexcept {
|
||||
mEnv = env;
|
||||
}
|
||||
|
||||
void* getData() const noexcept {
|
||||
return mUserData;
|
||||
}
|
||||
|
||||
size_t getSize() const noexcept {
|
||||
return mSize;
|
||||
}
|
||||
|
||||
size_t getShift() const noexcept {
|
||||
return mShift;
|
||||
}
|
||||
|
||||
size_t countToByte(size_t count) const noexcept {
|
||||
return count << mShift;
|
||||
}
|
||||
|
||||
private:
|
||||
void* mUserData = nullptr;
|
||||
size_t mSize = 0;
|
||||
BufferType mType = BufferType::BYTE;
|
||||
uint8_t mShift = 0;
|
||||
|
||||
JNIEnv* mEnv;
|
||||
void* mData = nullptr;
|
||||
jobject mBuffer = nullptr;
|
||||
jarray mBaseArray = nullptr;
|
||||
bool mDoCommit = false;
|
||||
|
||||
struct {
|
||||
jclass jniClass;
|
||||
jmethodID getBasePointer;
|
||||
jmethodID getBaseArray;
|
||||
jmethodID getBaseArrayOffset;
|
||||
jmethodID getBufferType;
|
||||
} mNioUtils{};
|
||||
};
|
||||
Reference in New Issue
Block a user