size_t wyAssetInputStream_android::getPosition() { if(m_asset != NULL) return gAAL.getAssetLength(m_asset) - gAAL.getAssetRemainingLength(m_asset); else if(m_fp != NULL) return ftell(m_fp); else return 0; }
void wyDirector_android::setScaleMode(wyScaleMode mode) { wyDirector::setScaleMode(mode); // need update AAL if(gAAL.setScaleMode) gAAL.setScaleMode((int)mode); }
ssize_t wyAssetInputStream_android::read(char* buffer, size_t length) { if(m_asset != NULL) return gAAL.readAsset(m_asset, buffer, length); else if(m_fp != NULL) return fread(buffer, sizeof(char), length, m_fp); else return -1; }
size_t wyAssetInputStream_android::available() { if(m_asset != NULL) return gAAL.getAssetRemainingLength(m_asset); else if(m_fp != NULL) return getLength() - getPosition(); else return 0; }
size_t wyAssetInputStream_android::seek(int offset, int mode) { if(m_asset != NULL) return gAAL.seekAsset(m_asset, offset, mode); else if(m_fp != NULL) return fseek(m_fp, offset, mode); else return 0; }
wyAssetInputStream_android::~wyAssetInputStream_android() { if(m_asset != NULL) { gAAL.closeAsset(m_asset); m_asset = NULL; } else if(m_fp != NULL) { fclose(m_fp); m_fp = NULL; } }
size_t wyAssetInputStream_android::seek(int offset, int mode) { if(m_asset != NULL) { return gAAL.seekAsset(m_asset, offset, mode); } else if(m_fp != NULL) { fseek(m_fp, offset, mode); return ftell(m_fp); } else { return 0; } }
char* wyAssetInputStream_android::getBuffer() { size_t len = getLength(); char* buf = (char*)wyMalloc(len * sizeof(char)); if(m_asset != NULL) memcpy(buf, gAAL.getAssetBuffer(m_asset), len); else if(m_fp != NULL) fread(buf, sizeof(char), len, m_fp); return buf; }
size_t wyAssetInputStream_android::getLength() { if(m_asset != NULL) return gAAL.getAssetLength(m_asset); else if(m_fp != NULL) { size_t offset = ftell(m_fp); fseek(m_fp, 0, SEEK_END); size_t len = ftell(m_fp); fseek(m_fp, offset, SEEK_SET); return len; } else { return 0; } }
wyAssetInputStream_android::wyAssetInputStream_android(const char* path, bool isFile) : wyAssetInputStream(path, isFile), m_asset(NULL), m_fp(NULL) { if(isFile) { // open file if((m_fp = fopen(path, "rb")) == NULL) { LOGW("open file %s failed: %s", path, strerror(errno)); m_fp = NULL; } } else { m_asset = gAAL.getAsset(path); } }
void wyDirector_android::attachContext(wyGLContext context) { if (m_context != context) { // get env JNIEnv* env = wyUtils::getJNIEnv(); // global init globalInit(env); // delete old reference if(m_context != NULL) { env->DeleteGlobalRef(m_context); m_context = NULL; } // save context reference if(context != NULL) m_context = env->NewGlobalRef(context); // setup sal setupAAL(); // get resources jobject res = env->CallObjectMethod(m_context, g_mid_Context_getResources); // get display metrics jobject dm = env->CallObjectMethod(res, g_mid_Resources_getDisplayMetrics); env->DeleteLocalRef(res); // set surface size wyDevice::realWidth = env->GetIntField(dm, g_fid_DisplayMetrics_widthPixels); wyDevice::realHeight = env->GetIntField(dm, g_fid_DisplayMetrics_heightPixels); if(SCALE_MODE_BY_DENSITY == wyDevice::scaleMode) { wyDevice::winWidth = wyDevice::realWidth; wyDevice::winHeight = wyDevice::realHeight; } // save other parameters wyDevice::density = env->GetFloatField(dm, g_fid_DisplayMetrics_density); wyDevice::scaledDensity = env->GetFloatField(dm, g_fid_DisplayMetrics_scaledDensity); // set density to aal if(gAAL.setDensity) gAAL.setDensity(wyDevice::density); // delete display metrics ref env->DeleteLocalRef(dm); } }
void wyDirector_android::setupAAL() { JNIEnv* env = wyUtils::getJNIEnv(); if(env != NULL) { // get package name jstring pkg = (jstring)env->CallObjectMethod(m_context, g_mid_Context_getPackageName); // build lib path const char* cPkg = env->GetStringUTFChars(pkg, NULL); // open android adapter layer so char buf[128]; if(wyDevice::apiLevel > 15) { sprintf(buf, "/data/data/%s/lib/libaal_jellybean.so", cPkg); } else if(wyDevice::apiLevel > 10) { sprintf(buf, "/data/data/%s/lib/libaal_honeycomb.so", cPkg); } else { sprintf(buf, "/data/data/%s/lib/libaal.so", cPkg); } sAALHandler = dlopen(buf, RTLD_LAZY); // release pkg env->ReleaseStringUTFChars(pkg, cPkg); // if failed to open sal in app data folder, try find them in system lib if(!sAALHandler) { if(wyDevice::apiLevel > 15) { sprintf(buf, "/system/lib/libaal_jellybean.so"); } else if(wyDevice::apiLevel > 10) { sprintf(buf, "/system/lib/libaal_honeycomb.so"); } else { sprintf(buf, "/system/lib/libaal.so"); } sAALHandler = dlopen(buf, RTLD_LAZY); } // check if (!sAALHandler) { LOGE("Cannot open android adapter layer"); exit(1); } // find functions gAAL.scaleImage = (scaleImageFunc)dlsym(sAALHandler, "scaleImage"); if(gAAL.scaleImage == NULL) { LOGE("Cannot load symbol 'scaleImage'"); dlclose(sAALHandler); exit(1); } gAAL.scalePVR = (scalePVRFunc)dlsym(sAALHandler, "scalePVR"); if(gAAL.scalePVR == NULL) { LOGE("Cannot load symbol 'scalePVR'"); dlclose(sAALHandler); exit(1); } gAAL.calculateTextSizeWithFont = (calculateTextSizeWithFontFunc)dlsym(sAALHandler, "calculateTextSizeWithFont"); if(gAAL.calculateTextSizeWithFont == NULL) { LOGE("Cannot load symbol 'calculateTextSizeWithFont'"); dlclose(sAALHandler); exit(1); } gAAL.calculateTextSizeWithCustomFont = (calculateTextSizeWithCustomFontFunc)dlsym(sAALHandler, "calculateTextSizeWithCustomFont"); if(gAAL.calculateTextSizeWithCustomFont == NULL) { LOGE("Cannot load symbol 'calculateTextSizeWithCustomFont'"); dlclose(sAALHandler); exit(1); } gAAL.createLabelBitmapWithFont = (createLabelBitmapWithFontFunc)dlsym(sAALHandler, "createLabelBitmapWithFont"); if(gAAL.createLabelBitmapWithFont == NULL) { LOGE("Cannot load symbol 'createLabelBitmapWithFont'"); dlclose(sAALHandler); exit(1); } gAAL.createLabelBitmapWithCustomFont = (createLabelBitmapWithCustomFontFunc)dlsym(sAALHandler, "createLabelBitmapWithCustomFont"); if(gAAL.createLabelBitmapWithCustomFont == NULL) { LOGE("Cannot load symbol 'createLabelBitmapWithCustomFont'"); dlclose(sAALHandler); exit(1); } gAAL.deinit = (deinitFunc)dlsym(sAALHandler, "deinit"); if(gAAL.deinit == NULL) { LOGE("Cannot load symbol 'deinit'"); dlclose(sAALHandler); exit(1); } gAAL.setEnv = (setEnvFunc)dlsym(sAALHandler, "setEnv"); if(gAAL.setEnv == NULL) { LOGE("Cannot load symbol 'setEnv'"); dlclose(sAALHandler); exit(1); } gAAL.setContext = (setContextFunc)dlsym(sAALHandler, "setContext"); if(gAAL.setContext == NULL) { LOGE("Cannot load symbol 'setContext'"); dlclose(sAALHandler); exit(1); } gAAL.setDensity = (setDensityFunc)dlsym(sAALHandler, "setDensity"); if(gAAL.setDensity == NULL) { LOGE("Cannot load symbol 'setDensity'"); dlclose(sAALHandler); exit(1); } gAAL.setScaleMode = (setScaleModeFunc)dlsym(sAALHandler, "setScaleMode"); if(gAAL.setScaleMode == NULL) { LOGE("Cannot load symbol 'setScaleMode'"); dlclose(sAALHandler); exit(1); } gAAL.getAsset = (getAssetFunc)dlsym(sAALHandler, "getAsset"); if(gAAL.getAsset == NULL) { LOGE("Cannot load symbol 'getAsset'"); dlclose(sAALHandler); exit(1); } gAAL.getAssetByResId = (getAssetByResIdFunc)dlsym(sAALHandler, "getAssetByResId"); if(gAAL.getAssetByResId == NULL) { LOGE("Cannot load symbol 'getAssetByResId'"); dlclose(sAALHandler); exit(1); } gAAL.getAssetBuffer = (getAssetBufferFunc)dlsym(sAALHandler, "getAssetBuffer"); if(gAAL.getAssetBuffer == NULL) { LOGE("Cannot load symbol 'getAssetBuffer'"); dlclose(sAALHandler); exit(1); } gAAL.getAssetLength = (getAssetLengthFunc)dlsym(sAALHandler, "getAssetLength"); if(gAAL.getAssetLength == NULL) { LOGE("Cannot load symbol 'getAssetLength'"); dlclose(sAALHandler); exit(1); } gAAL.getAssetRemainingLength = (getAssetRemainingLengthFunc)dlsym(sAALHandler, "getAssetRemainingLength"); if(gAAL.getAssetRemainingLength == NULL) { LOGE("Cannot load symbol 'getAssetRemainingLength'"); dlclose(sAALHandler); exit(1); } gAAL.readAsset = (readAssetFunc)dlsym(sAALHandler, "readAsset"); if(gAAL.readAsset == NULL) { LOGE("Cannot load symbol 'readAsset'"); dlclose(sAALHandler); exit(1); } gAAL.seekAsset = (seekAssetFunc)dlsym(sAALHandler, "seekAsset"); if(gAAL.seekAsset == NULL) { LOGE("Cannot load symbol 'seekAsset'"); dlclose(sAALHandler); exit(1); } gAAL.closeAsset = (closeAssetFunc)dlsym(sAALHandler, "closeAsset"); if(gAAL.closeAsset == NULL) { LOGE("Cannot load symbol 'closeAsset'"); dlclose(sAALHandler); exit(1); } gAAL.getResFD = (getResFDFunc)dlsym(sAALHandler, "getResFD"); if(gAAL.getResFD == NULL) { LOGE("Cannot load symbol 'getResFD'"); dlclose(sAALHandler); exit(1); } gAAL.getAssetFD = (getAssetFDFunc)dlsym(sAALHandler, "getAssetFD"); if(gAAL.getAssetFD == NULL) { LOGE("Cannot load symbol 'getAssetFD'"); dlclose(sAALHandler); exit(1); } gAAL.utf16toutf8 = (utf16toutf8Func)dlsym(sAALHandler, "utf16toutf8"); if(gAAL.utf16toutf8 == NULL) { LOGE("Cannot load symbol 'utf16toutf8'"); dlclose(sAALHandler); exit(1); } gAAL.toUTF16 = (toUTF16Func)dlsym(sAALHandler, "toUTF16"); if(gAAL.toUTF16 == NULL) { LOGE("Cannot load symbol 'toUTF16'"); dlclose(sAALHandler); exit(1); } gAAL.wctoutf8 = (wctoutf8Func)dlsym(sAALHandler, "wctoutf8"); if(gAAL.wctoutf8 == NULL) { LOGE("Cannot load symbol 'wctoutf8'"); dlclose(sAALHandler); exit(1); } // set env to aal if(gAAL.setEnv) gAAL.setEnv(env); // set context to aal if(gAAL.setContext) gAAL.setContext(m_context); } }
wyDirector_android::~wyDirector_android() { // if background running is enabled if(m_allowBackgroundRunning) { pthread_mutex_lock(&gCondMutex); if(m_backgroundRunning) { if(pthread_cond_init(&sBackgroundLooperCond, NULL) == 0) { m_backgroundRunning = false; pthread_cond_wait(&sBackgroundLooperCond, &gCondMutex); pthread_cond_destroy(&sBackgroundLooperCond); } } pthread_mutex_unlock(&gCondMutex); } // call gl view JNIEnv* env = wyUtils::getJNIEnv(); if(env != NULL) { if(m_glView != NULL) { if(!m_paused) env->CallVoidMethod(m_glView, g_mid_WYGLSurfaceView_onPause); env->DeleteGlobalRef(m_glView); m_glView = NULL; } } // if resource decoder is set, check whether it is wyJavaResourceDecoder if(gResDecoder) { wyJavaResourceDecoder* jrd = dynamic_cast<wyJavaResourceDecoder*>(gResDecoder); if(jrd) delete jrd; } // common destroy commonDestroy(); // release listener wyArrayEach(m_jLifecycleListeners, j_releaseListener, NULL); wyArrayDestroy(m_jLifecycleListeners); // deinit aal if(gAAL.deinit) gAAL.deinit(); // delete context ref if(env != NULL) { if(m_context != NULL) { env->DeleteGlobalRef(m_context); m_context = NULL; } } // global deinit globalDeInit(env); // unload sal lib if(sAALHandler != NULL) { /* * 不要close这个东西, 可能是因为android底层有什么bug, 导致重复载入卸载45 * 次之后就会出现问题. 而android上的dlopen又不支持RTLD_NODELETE这个flag, * 所以只能不close它, 就没事了 */ // dlclose(sAALHandler); sAALHandler = NULL; } // nullify gVM gVM = NULL; // free my self gDirector = NULL; }
wyAssetInputStream_android::wyAssetInputStream_android(int resId) : wyAssetInputStream(resId), m_asset(NULL), m_fp(NULL) { m_asset = gAAL.getAssetByResId(resId, NULL); }