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