in PlayAssetDelivery/NativeSample/common/ndk_helper/JNIHelper.cpp [220:281]
uint32_t JNIHelper::LoadTexture(const char* file_name, int32_t* outWidth,
int32_t* outHeight, bool* hasAlpha) {
if (activity_ == NULL) {
LOGI(
"JNIHelper has not been initialized. Call init() to initialize the "
"helper");
return 0;
}
// Lock mutex
std::lock_guard<std::mutex> lock(mutex_);
JNIEnv* env = AttachCurrentThread();
jstring name = env->NewStringUTF(file_name);
GLuint tex;
glGenTextures(1, &tex);
glBindTexture(GL_TEXTURE_2D, tex);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
GL_LINEAR_MIPMAP_NEAREST);
glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
jmethodID mid = env->GetMethodID(jni_helper_java_class_, "loadTexture",
"(Ljava/lang/String;)Ljava/lang/Object;");
jobject out = env->CallObjectMethod(jni_helper_java_ref_, mid, name);
jclass javaCls =
RetrieveClass(env, "com/sample/helper/NDKHelper$TextureInformation");
jfieldID fidRet = env->GetFieldID(javaCls, "ret", "Z");
jfieldID fidHasAlpha = env->GetFieldID(javaCls, "alphaChannel", "Z");
jfieldID fidWidth = env->GetFieldID(javaCls, "originalWidth", "I");
jfieldID fidHeight = env->GetFieldID(javaCls, "originalHeight", "I");
bool ret = env->GetBooleanField(out, fidRet);
bool alpha = env->GetBooleanField(out, fidHasAlpha);
int32_t width = env->GetIntField(out, fidWidth);
int32_t height = env->GetIntField(out, fidHeight);
if (!ret) {
glDeleteTextures(1, &tex);
tex = -1;
LOGI("Texture load failed %s", file_name);
}
LOGI("Loaded texture original size:%dx%d alpha:%d", width, height,
(int32_t)alpha);
if (outWidth != NULL) {
*outWidth = width;
}
if (outHeight != NULL) {
*outHeight = height;
}
if (hasAlpha != NULL) {
*hasAlpha = alpha;
}
// Generate mipmap
glGenerateMipmap(GL_TEXTURE_2D);
env->DeleteLocalRef(name);
DetachCurrentThread();
return tex;
}