MCPcopy Create free account
hub / github.com/android/ndk-samples / LoadTexture

Method LoadTexture

teapots/common/ndk_helper/JNIHelper.cpp:219–280  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

217}
218
219uint32_t JNIHelper::LoadTexture(const char* file_name, int32_t* outWidth,
220 int32_t* outHeight, bool* hasAlpha) {
221 if (activity_ == NULL) {
222 LOGI(
223 "JNIHelper has not been initialized. Call init() to initialize the "
224 "helper");
225 return 0;
226 }
227
228 // Lock mutex
229 std::lock_guard<std::mutex> lock(mutex_);
230
231 JNIEnv* env = AttachCurrentThread();
232 jstring name = env->NewStringUTF(file_name);
233
234 GLuint tex;
235 glGenTextures(1, &tex);
236 glBindTexture(GL_TEXTURE_2D, tex);
237
238 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER,
239 GL_LINEAR_MIPMAP_NEAREST);
240 glTexParameterf(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR);
241 jmethodID mid = env->GetMethodID(jni_helper_java_class_, "loadTexture",
242 "(Ljava/lang/String;)Ljava/lang/Object;");
243
244 jobject out = env->CallObjectMethod(jni_helper_java_ref_, mid, name);
245
246 jclass javaCls =
247 RetrieveClass(env, "com/sample/helper/NDKHelper$TextureInformation");
248 jfieldID fidRet = env->GetFieldID(javaCls, "ret", "Z");
249 jfieldID fidHasAlpha = env->GetFieldID(javaCls, "alphaChannel", "Z");
250 jfieldID fidWidth = env->GetFieldID(javaCls, "originalWidth", "I");
251 jfieldID fidHeight = env->GetFieldID(javaCls, "originalHeight", "I");
252 bool ret = env->GetBooleanField(out, fidRet);
253 bool alpha = env->GetBooleanField(out, fidHasAlpha);
254 int32_t width = env->GetIntField(out, fidWidth);
255 int32_t height = env->GetIntField(out, fidHeight);
256 if (!ret) {
257 glDeleteTextures(1, &tex);
258 tex = -1;
259 LOGI("Texture load failed %s", file_name);
260 }
261 LOGI("Loaded texture original size:%dx%d alpha:%d", width, height,
262 (int32_t)alpha);
263 if (outWidth != NULL) {
264 *outWidth = width;
265 }
266 if (outHeight != NULL) {
267 *outHeight = height;
268 }
269 if (hasAlpha != NULL) {
270 *hasAlpha = alpha;
271 }
272
273 // Generate mipmap
274 glGenerateMipmap(GL_TEXTURE_2D);
275
276 env->DeleteLocalRef(name);

Callers

nothing calls this directly

Calls 1

CallObjectMethodMethod · 0.80

Tested by

no test coverage detected