jobject JNIHelper::LoadImage()

in PlayAssetDelivery/NativeSample/common/ndk_helper/JNIHelper.cpp [338:391]


jobject JNIHelper::LoadImage(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);

  jmethodID mid = env->GetMethodID(jni_helper_java_class_, "loadImage",
                                   "(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) {
    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;
  }

  jfieldID fidImage = env->GetFieldID(javaCls, "image", "Ljava/lang/Object;");
  jobject array = env->GetObjectField(out, fidImage);
  jobject objGlobal = env->NewGlobalRef(array);

  env->DeleteLocalRef(name);
  env->DeleteLocalRef(javaCls);

  return objGlobal;
}