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