1 /* libs/android_runtime/android/graphics/ColorFilter.cpp 2 ** 3 ** Copyright 2006, The Android Open Source Project 4 ** 5 ** Licensed under the Apache License, Version 2.0 (the "License"); 6 ** you may not use this file except in compliance with the License. 7 ** You may obtain a copy of the License at 8 ** 9 ** http://www.apache.org/licenses/LICENSE-2.0 10 ** 11 ** Unless required by applicable law or agreed to in writing, software 12 ** distributed under the License is distributed on an "AS IS" BASIS, 13 ** WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 14 ** See the License for the specific language governing permissions and 15 ** limitations under the License. 16 */ 17 18 #include "jni.h" 19 #include "GraphicsJNI.h" 20 #include <android_runtime/AndroidRuntime.h> 21 22 #include "SkColorFilter.h" 23 #include "SkColorMatrixFilter.h" 24 #include "SkPorterDuff.h" 25 26 namespace android { 27 28 class SkColorFilterGlue { 29 public: 30 31 static void finalizer(JNIEnv* env, jobject clazz, SkColorFilter* obj) { 32 obj->safeUnref(); 33 } 34 35 static SkColorFilter* CreatePorterDuffFilter(JNIEnv* env, jobject, 36 jint srcColor, SkPorterDuff::Mode mode) { 37 return SkColorFilter::CreateModeFilter(srcColor, 38 SkPorterDuff::ToXfermodeMode(mode)); 39 } 40 41 static SkColorFilter* CreateLightingFilter(JNIEnv* env, jobject, 42 jint mul, jint add) { 43 return SkColorFilter::CreateLightingFilter(mul, add); 44 } 45 46 static SkColorFilter* CreateColorMatrixFilter(JNIEnv* env, jobject, 47 jfloatArray jarray) { 48 AutoJavaFloatArray autoArray(env, jarray, 20); 49 const float* src = autoArray.ptr(); 50 51 #ifdef SK_SCALAR_IS_FIXED 52 SkFixed array[20]; 53 for (int i = 0; i < 20; i++) { 54 array[i] = SkFloatToScalar(src[i]); 55 } 56 return new SkColorMatrixFilter(array); 57 #else 58 return new SkColorMatrixFilter(src); 59 #endif 60 } 61 62 }; 63 64 static JNINativeMethod colorfilter_methods[] = { 65 {"finalizer", "(I)V", (void*) SkColorFilterGlue::finalizer} 66 }; 67 68 static JNINativeMethod porterduff_methods[] = { 69 {"native_CreatePorterDuffFilter","(II)I", 70 (void*) SkColorFilterGlue::CreatePorterDuffFilter} 71 }; 72 73 static JNINativeMethod lighting_methods[] = { 74 {"native_CreateLightingFilter","(II)I", 75 (void*) SkColorFilterGlue::CreateLightingFilter} 76 }; 77 78 static JNINativeMethod colormatrix_methods[] = { 79 {"nativeColorMatrixFilter","([F)I", 80 (void*) SkColorFilterGlue::CreateColorMatrixFilter} 81 }; 82 83 #define REG(env, name, array) \ 84 result = android::AndroidRuntime::registerNativeMethods(env, name, array, \ 85 SK_ARRAY_COUNT(array)); \ 86 if (result < 0) return result 87 88 89 int register_android_graphics_ColorFilter(JNIEnv* env) { 90 int result; 91 92 REG(env, "android/graphics/ColorFilter", colorfilter_methods); 93 REG(env, "android/graphics/PorterDuffColorFilter", porterduff_methods); 94 REG(env, "android/graphics/LightingColorFilter", lighting_methods); 95 REG(env, "android/graphics/ColorMatrixColorFilter", colormatrix_methods); 96 97 return 0; 98 } 99 100 } 101