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 "core_jni_helpers.h" 21 22 #include "SkColorFilter.h" 23 #include "SkColorMatrixFilter.h" 24 #include "SkXfermode.h" 25 26 #include <Caches.h> 27 28 namespace android { 29 30 using namespace uirenderer; 31 32 class SkColorFilterGlue { 33 public: 34 static void finalizer(JNIEnv* env, jobject clazz, jlong skFilterHandle) { 35 SkColorFilter* filter = reinterpret_cast<SkColorFilter *>(skFilterHandle); 36 if (filter) SkSafeUnref(filter); 37 } 38 39 static jlong CreatePorterDuffFilter(JNIEnv* env, jobject, jint srcColor, jint modeHandle) { 40 SkXfermode::Mode mode = static_cast<SkXfermode::Mode>(modeHandle); 41 return reinterpret_cast<jlong>(SkColorFilter::CreateModeFilter(srcColor, mode)); 42 } 43 44 static jlong CreateLightingFilter(JNIEnv* env, jobject, jint mul, jint add) { 45 return reinterpret_cast<jlong>(SkColorFilter::CreateLightingFilter(mul, add)); 46 } 47 48 static jlong CreateColorMatrixFilter(JNIEnv* env, jobject, jfloatArray jarray) { 49 AutoJavaFloatArray autoArray(env, jarray, 20); 50 const float* src = autoArray.ptr(); 51 52 #ifdef SK_SCALAR_IS_FLOAT 53 return reinterpret_cast<jlong>(SkColorMatrixFilter::Create(src)); 54 #else 55 SkASSERT(false); 56 #endif 57 } 58 }; 59 60 static JNINativeMethod colorfilter_methods[] = { 61 {"destroyFilter", "(J)V", (void*) SkColorFilterGlue::finalizer} 62 }; 63 64 static JNINativeMethod porterduff_methods[] = { 65 { "native_CreatePorterDuffFilter", "(II)J", (void*) SkColorFilterGlue::CreatePorterDuffFilter }, 66 }; 67 68 static JNINativeMethod lighting_methods[] = { 69 { "native_CreateLightingFilter", "(II)J", (void*) SkColorFilterGlue::CreateLightingFilter }, 70 }; 71 72 static JNINativeMethod colormatrix_methods[] = { 73 { "nativeColorMatrixFilter", "([F)J", (void*) SkColorFilterGlue::CreateColorMatrixFilter }, 74 }; 75 76 int register_android_graphics_ColorFilter(JNIEnv* env) { 77 android::RegisterMethodsOrDie(env, "android/graphics/ColorFilter", colorfilter_methods, 78 NELEM(colorfilter_methods)); 79 android::RegisterMethodsOrDie(env, "android/graphics/PorterDuffColorFilter", porterduff_methods, 80 NELEM(porterduff_methods)); 81 android::RegisterMethodsOrDie(env, "android/graphics/LightingColorFilter", lighting_methods, 82 NELEM(lighting_methods)); 83 android::RegisterMethodsOrDie(env, "android/graphics/ColorMatrixColorFilter", 84 colormatrix_methods, NELEM(colormatrix_methods)); 85 86 return 0; 87 } 88 89 } 90