Home | History | Annotate | Download | only in graphics
      1 #include "GraphicsJNI.h"
      2 #include "Sk1DPathEffect.h"
      3 #include "SkCornerPathEffect.h"
      4 #include "SkDashPathEffect.h"
      5 #include "SkDiscretePathEffect.h"
      6 #include "SkPathEffect.h"
      7 #include "core_jni_helpers.h"
      8 
      9 #include <jni.h>
     10 
     11 class SkPathEffectGlue {
     12 public:
     13 
     14     static void destructor(JNIEnv* env, jobject, jlong effectHandle) {
     15         SkPathEffect* effect = reinterpret_cast<SkPathEffect*>(effectHandle);
     16         SkSafeUnref(effect);
     17     }
     18 
     19     static jlong Compose_constructor(JNIEnv* env, jobject,
     20                                      jlong outerHandle, jlong innerHandle) {
     21         SkPathEffect* outer = reinterpret_cast<SkPathEffect*>(outerHandle);
     22         SkPathEffect* inner = reinterpret_cast<SkPathEffect*>(innerHandle);
     23         SkPathEffect* effect = SkPathEffect::MakeCompose(sk_ref_sp(outer),
     24                 sk_ref_sp(inner)).release();
     25         return reinterpret_cast<jlong>(effect);
     26     }
     27 
     28     static jlong Sum_constructor(JNIEnv* env, jobject,
     29                                  jlong firstHandle, jlong secondHandle) {
     30         SkPathEffect* first = reinterpret_cast<SkPathEffect*>(firstHandle);
     31         SkPathEffect* second = reinterpret_cast<SkPathEffect*>(secondHandle);
     32         SkPathEffect* effect = SkPathEffect::MakeSum(sk_ref_sp(first),
     33                 sk_ref_sp(second)).release();
     34         return reinterpret_cast<jlong>(effect);
     35     }
     36 
     37     static jlong Dash_constructor(JNIEnv* env, jobject,
     38                                       jfloatArray intervalArray, jfloat phase) {
     39         AutoJavaFloatArray autoInterval(env, intervalArray);
     40         int         count = autoInterval.length() & ~1;  // even number
     41 #ifdef SK_SCALAR_IS_FLOAT
     42         SkScalar*   intervals = autoInterval.ptr();
     43 #else
     44         #error Need to convert float array to SkScalar array before calling the following function.
     45 #endif
     46         SkPathEffect* effect = SkDashPathEffect::Make(intervals, count, phase).release();
     47         return reinterpret_cast<jlong>(effect);
     48     }
     49 
     50     static jlong OneD_constructor(JNIEnv* env, jobject,
     51                   jlong shapeHandle, jfloat advance, jfloat phase, jint style) {
     52         const SkPath* shape = reinterpret_cast<SkPath*>(shapeHandle);
     53         SkASSERT(shape != NULL);
     54         SkPathEffect* effect = SkPath1DPathEffect::Make(*shape, advance, phase,
     55                 (SkPath1DPathEffect::Style)style).release();
     56         return reinterpret_cast<jlong>(effect);
     57     }
     58 
     59     static jlong Corner_constructor(JNIEnv* env, jobject, jfloat radius){
     60         SkPathEffect* effect = SkCornerPathEffect::Make(radius).release();
     61         return reinterpret_cast<jlong>(effect);
     62     }
     63 
     64     static jlong Discrete_constructor(JNIEnv* env, jobject,
     65                                       jfloat length, jfloat deviation) {
     66         SkPathEffect* effect = SkDiscretePathEffect::Make(length, deviation).release();
     67         return reinterpret_cast<jlong>(effect);
     68     }
     69 
     70 };
     71 
     72 ////////////////////////////////////////////////////////////////////////////////////////////////////////
     73 
     74 static const JNINativeMethod gPathEffectMethods[] = {
     75     { "nativeDestructor", "(J)V", (void*)SkPathEffectGlue::destructor }
     76 };
     77 
     78 static const JNINativeMethod gComposePathEffectMethods[] = {
     79     { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Compose_constructor }
     80 };
     81 
     82 static const JNINativeMethod gSumPathEffectMethods[] = {
     83     { "nativeCreate", "(JJ)J", (void*)SkPathEffectGlue::Sum_constructor }
     84 };
     85 
     86 static const JNINativeMethod gDashPathEffectMethods[] = {
     87     { "nativeCreate", "([FF)J", (void*)SkPathEffectGlue::Dash_constructor }
     88 };
     89 
     90 static const JNINativeMethod gPathDashPathEffectMethods[] = {
     91     { "nativeCreate", "(JFFI)J", (void*)SkPathEffectGlue::OneD_constructor }
     92 };
     93 
     94 static const JNINativeMethod gCornerPathEffectMethods[] = {
     95     { "nativeCreate", "(F)J", (void*)SkPathEffectGlue::Corner_constructor }
     96 };
     97 
     98 static const JNINativeMethod gDiscretePathEffectMethods[] = {
     99     { "nativeCreate", "(FF)J", (void*)SkPathEffectGlue::Discrete_constructor }
    100 };
    101 
    102 int register_android_graphics_PathEffect(JNIEnv* env)
    103 {
    104     android::RegisterMethodsOrDie(env, "android/graphics/PathEffect", gPathEffectMethods,
    105                          NELEM(gPathEffectMethods));
    106     android::RegisterMethodsOrDie(env, "android/graphics/ComposePathEffect",
    107                                   gComposePathEffectMethods, NELEM(gComposePathEffectMethods));
    108     android::RegisterMethodsOrDie(env, "android/graphics/SumPathEffect", gSumPathEffectMethods,
    109                                   NELEM(gSumPathEffectMethods));
    110     android::RegisterMethodsOrDie(env, "android/graphics/DashPathEffect", gDashPathEffectMethods,
    111                                   NELEM(gDashPathEffectMethods));
    112     android::RegisterMethodsOrDie(env, "android/graphics/PathDashPathEffect",
    113                                   gPathDashPathEffectMethods, NELEM(gPathDashPathEffectMethods));
    114     android::RegisterMethodsOrDie(env, "android/graphics/CornerPathEffect",
    115                                   gCornerPathEffectMethods, NELEM(gCornerPathEffectMethods));
    116     android::RegisterMethodsOrDie(env, "android/graphics/DiscretePathEffect",
    117                                   gDiscretePathEffectMethods, NELEM(gDiscretePathEffectMethods));
    118 
    119     return 0;
    120 }
    121