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