am 73205a3a: am bb005f78: Start using JNI\'s Get<Type>ArrayRegion()

* commit '73205a3a1f7ddf7bacce8c78894e245a6ab654ba':
  Start using JNI's Get<Type>ArrayRegion()
main
Ken Wakasa 2012-08-08 07:45:05 -07:00 committed by Android Git Automerger
commit f285abcc19
4 changed files with 48 additions and 90 deletions

View File

@ -25,43 +25,21 @@ namespace latinime {
static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object, static jlong latinime_Keyboard_setProximityInfo(JNIEnv *env, jobject object,
jstring localeJStr, jint maxProximityCharsSize, jint displayWidth, jint displayHeight, jstring localeJStr, jint maxProximityCharsSize, jint displayWidth, jint displayHeight,
jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityCharsArray, jint gridWidth, jint gridHeight, jint mostCommonkeyWidth, jintArray proximityChars,
jint keyCount, jintArray keyXCoordinateArray, jintArray keyYCoordinateArray, jint keyCount, jintArray keyXCoordinates, jintArray keyYCoordinates,
jintArray keyWidthArray, jintArray keyHeightArray, jintArray keyCharCodeArray, jintArray keyWidths, jintArray keyHeights, jintArray keyCharCodes,
jfloatArray sweetSpotCenterXArray, jfloatArray sweetSpotCenterYArray, jfloatArray sweetSpotCenterXs, jfloatArray sweetSpotCenterYs, jfloatArray sweetSpotRadii) {
jfloatArray sweetSpotRadiusArray) {
const char *localeCStr = env->GetStringUTFChars(localeJStr, 0); const char *localeCStr = env->GetStringUTFChars(localeJStr, 0);
jint *proximityChars = env->GetIntArrayElements(proximityCharsArray, 0); ProximityInfo *proximityInfo = new ProximityInfo(env, localeCStr, maxProximityCharsSize,
jint *keyXCoordinates = safeGetIntArrayElements(env, keyXCoordinateArray); displayWidth, displayHeight, gridWidth, gridHeight, mostCommonkeyWidth, proximityChars,
jint *keyYCoordinates = safeGetIntArrayElements(env, keyYCoordinateArray); keyCount, keyXCoordinates, keyYCoordinates, keyWidths, keyHeights, keyCharCodes,
jint *keyWidths = safeGetIntArrayElements(env, keyWidthArray); sweetSpotCenterXs, sweetSpotCenterYs, sweetSpotRadii);
jint *keyHeights = safeGetIntArrayElements(env, keyHeightArray);
jint *keyCharCodes = safeGetIntArrayElements(env, keyCharCodeArray);
jfloat *sweetSpotCenterXs = safeGetFloatArrayElements(env, sweetSpotCenterXArray);
jfloat *sweetSpotCenterYs = safeGetFloatArrayElements(env, sweetSpotCenterYArray);
jfloat *sweetSpotRadii = safeGetFloatArrayElements(env, sweetSpotRadiusArray);
ProximityInfo *proximityInfo = new ProximityInfo(
localeCStr, maxProximityCharsSize, displayWidth, displayHeight, gridWidth, gridHeight,
mostCommonkeyWidth, (const int32_t*)proximityChars, keyCount,
(const int32_t*)keyXCoordinates, (const int32_t*)keyYCoordinates,
(const int32_t*)keyWidths, (const int32_t*)keyHeights, (const int32_t*)keyCharCodes,
(const float*)sweetSpotCenterXs, (const float*)sweetSpotCenterYs,
(const float*)sweetSpotRadii);
safeReleaseFloatArrayElements(env, sweetSpotRadiusArray, sweetSpotRadii);
safeReleaseFloatArrayElements(env, sweetSpotCenterYArray, sweetSpotCenterYs);
safeReleaseFloatArrayElements(env, sweetSpotCenterXArray, sweetSpotCenterXs);
safeReleaseIntArrayElements(env, keyCharCodeArray, keyCharCodes);
safeReleaseIntArrayElements(env, keyHeightArray, keyHeights);
safeReleaseIntArrayElements(env, keyWidthArray, keyWidths);
safeReleaseIntArrayElements(env, keyYCoordinateArray, keyYCoordinates);
safeReleaseIntArrayElements(env, keyXCoordinateArray, keyXCoordinates);
env->ReleaseIntArrayElements(proximityCharsArray, proximityChars, 0);
env->ReleaseStringUTFChars(localeJStr, localeCStr); env->ReleaseStringUTFChars(localeJStr, localeCStr);
return (jlong)proximityInfo; return reinterpret_cast<jlong>(proximityInfo);
} }
static void latinime_Keyboard_release(JNIEnv *env, jobject object, jlong proximityInfo) { static void latinime_Keyboard_release(JNIEnv *env, jobject object, jlong proximityInfo) {
ProximityInfo *pi = (ProximityInfo*)proximityInfo; ProximityInfo *pi = reinterpret_cast<ProximityInfo*>(proximityInfo);
if (!pi) return; if (!pi) return;
delete pi; delete pi;
} }

View File

@ -24,32 +24,5 @@ namespace latinime {
int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods, int registerNativeMethods(JNIEnv *env, const char *className, JNINativeMethod *methods,
int numMethods); int numMethods);
inline jint *safeGetIntArrayElements(JNIEnv *env, jintArray jArray) {
if (jArray) {
return env->GetIntArrayElements(jArray, 0);
} else {
return 0;
}
}
inline jfloat *safeGetFloatArrayElements(JNIEnv *env, jfloatArray jArray) {
if (jArray) {
return env->GetFloatArrayElements(jArray, 0);
} else {
return 0;
}
}
inline void safeReleaseIntArrayElements(JNIEnv *env, jintArray jArray, jint *cArray) {
if (jArray) {
env->ReleaseIntArrayElements(jArray, cArray, 0);
}
}
inline void safeReleaseFloatArrayElements(JNIEnv *env, jfloatArray jArray, jfloat *cArray) {
if (jArray) {
env->ReleaseFloatArrayElements(jArray, cArray, 0);
}
}
} // namespace latinime } // namespace latinime
#endif // LATINIME_JNI_COMMON_H #endif // LATINIME_JNI_COMMON_H

View File

@ -24,25 +24,36 @@
#include "additional_proximity_chars.h" #include "additional_proximity_chars.h"
#include "char_utils.h" #include "char_utils.h"
#include "defines.h" #include "defines.h"
#include "jni.h"
#include "proximity_info.h" #include "proximity_info.h"
namespace latinime { namespace latinime {
inline void copyOrFillZero(void *to, const void *from, size_t size) { static inline void safeGetOrFillZeroIntArrayRegion(JNIEnv *env, jintArray jArray, jsize len,
if (from) { jint *buffer) {
memcpy(to, from, size); if (jArray && buffer) {
} else { env->GetIntArrayRegion(jArray, 0, len, buffer);
memset(to, 0, size); } else if (buffer) {
memset(buffer, 0, len);
} }
} }
ProximityInfo::ProximityInfo(const char *localeCStr, const int maxProximityCharsSize, static inline void safeGetOrFillZeroFloatArrayRegion(JNIEnv *env, jfloatArray jArray, jsize len,
jfloat *buffer) {
if (jArray && buffer) {
env->GetFloatArrayRegion(jArray, 0, len, buffer);
} else if (buffer) {
memset(buffer, 0, len);
}
}
ProximityInfo::ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize,
const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int keyboardWidth, const int keyboardHeight, const int gridWidth,
const int gridHeight, const int mostCommonKeyWidth, const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars,
const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates,
const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes,
const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const float *sweetSpotCenterYs, const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs,
const float *sweetSpotRadii) const jfloatArray sweetSpotRadii)
: MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth), : MAX_PROXIMITY_CHARS_SIZE(maxProximityCharsSize), KEYBOARD_WIDTH(keyboardWidth),
KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight), KEYBOARD_HEIGHT(keyboardHeight), GRID_WIDTH(gridWidth), GRID_HEIGHT(gridHeight),
MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth), MOST_COMMON_KEY_WIDTH_SQUARE(mostCommonKeyWidth * mostCommonKeyWidth),
@ -58,20 +69,15 @@ ProximityInfo::ProximityInfo(const char *localeCStr, const int maxProximityChars
AKLOGI("Create proximity info array %d", proximityGridLength); AKLOGI("Create proximity info array %d", proximityGridLength);
} }
mProximityCharsArray = new int32_t[proximityGridLength]; mProximityCharsArray = new int32_t[proximityGridLength];
memcpy(mProximityCharsArray, proximityCharsArray, safeGetOrFillZeroIntArrayRegion(env, proximityChars, proximityGridLength, mProximityCharsArray);
proximityGridLength * sizeof(mProximityCharsArray[0])); safeGetOrFillZeroIntArrayRegion(env, keyXCoordinates, KEY_COUNT, mKeyXCoordinates);
safeGetOrFillZeroIntArrayRegion(env, keyYCoordinates, KEY_COUNT, mKeyYCoordinates);
copyOrFillZero(mKeyXCoordinates, keyXCoordinates, KEY_COUNT * sizeof(mKeyXCoordinates[0])); safeGetOrFillZeroIntArrayRegion(env, keyWidths, KEY_COUNT, mKeyWidths);
copyOrFillZero(mKeyYCoordinates, keyYCoordinates, KEY_COUNT * sizeof(mKeyYCoordinates[0])); safeGetOrFillZeroIntArrayRegion(env, keyHeights, KEY_COUNT, mKeyHeights);
copyOrFillZero(mKeyWidths, keyWidths, KEY_COUNT * sizeof(mKeyWidths[0])); safeGetOrFillZeroIntArrayRegion(env, keyCharCodes, KEY_COUNT, mKeyCharCodes);
copyOrFillZero(mKeyHeights, keyHeights, KEY_COUNT * sizeof(mKeyHeights[0])); safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterXs, KEY_COUNT, mSweetSpotCenterXs);
copyOrFillZero(mKeyCharCodes, keyCharCodes, KEY_COUNT * sizeof(mKeyCharCodes[0])); safeGetOrFillZeroFloatArrayRegion(env, sweetSpotCenterYs, KEY_COUNT, mSweetSpotCenterYs);
copyOrFillZero(mSweetSpotCenterXs, sweetSpotCenterXs, safeGetOrFillZeroFloatArrayRegion(env, sweetSpotRadii, KEY_COUNT, mSweetSpotRadii);
KEY_COUNT * sizeof(mSweetSpotCenterXs[0]));
copyOrFillZero(mSweetSpotCenterYs, sweetSpotCenterYs,
KEY_COUNT * sizeof(mSweetSpotCenterYs[0]));
copyOrFillZero(mSweetSpotRadii, sweetSpotRadii, KEY_COUNT * sizeof(mSweetSpotRadii[0]));
initializeCodeToKeyIndex(); initializeCodeToKeyIndex();
} }

View File

@ -21,6 +21,7 @@
#include <string> #include <string>
#include "defines.h" #include "defines.h"
#include "jni.h"
namespace latinime { namespace latinime {
@ -28,13 +29,13 @@ class Correction;
class ProximityInfo { class ProximityInfo {
public: public:
ProximityInfo(const char *localeCStr, const int maxProximityCharsSize, ProximityInfo(JNIEnv *env, const char *localeCStr, const int maxProximityCharsSize,
const int keyboardWidth, const int keyboardHeight, const int gridWidth, const int keyboardWidth, const int keyboardHeight, const int gridWidth,
const int gridHeight, const int mostCommonkeyWidth, const int gridHeight, const int mostCommonKeyWidth, const jintArray proximityChars,
const int32_t *proximityCharsArray, const int keyCount, const int32_t *keyXCoordinates, const int keyCount, const jintArray keyXCoordinates, const jintArray keyYCoordinates,
const int32_t *keyYCoordinates, const int32_t *keyWidths, const int32_t *keyHeights, const jintArray keyWidths, const jintArray keyHeights, const jintArray keyCharCodes,
const int32_t *keyCharCodes, const float *sweetSpotCenterXs, const jfloatArray sweetSpotCenterXs, const jfloatArray sweetSpotCenterYs,
const float *sweetSpotCenterYs, const float *sweetSpotRadii); const jfloatArray sweetSpotRadii);
~ProximityInfo(); ~ProximityInfo();
bool hasSpaceProximity(const int x, const int y) const; bool hasSpaceProximity(const int x, const int y) const;
int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const; int getNormalizedSquaredDistance(const int inputIndex, const int proximityIndex) const;