The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 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" |
Mike Reed | 47c0d4e | 2009-06-22 22:53:52 -0400 | [diff] [blame] | 24 | #include "SkPorterDuff.h" |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 25 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 26 | #include <SkiaColorFilter.h> |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 27 | #include <Caches.h> |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 28 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 29 | namespace android { |
| 30 | |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 31 | using namespace uirenderer; |
| 32 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 33 | class SkColorFilterGlue { |
| 34 | public: |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 35 | static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle, jlong fHandle) { |
| 36 | SkColorFilter* obj = reinterpret_cast<SkColorFilter *>(objHandle); |
| 37 | SkiaColorFilter* f = reinterpret_cast<SkiaColorFilter *>(fHandle); |
Romain Guy | 1365674 | 2013-08-19 17:42:51 -0700 | [diff] [blame] | 38 | if (obj) SkSafeUnref(obj); |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 39 | // f == NULL when not !USE_OPENGL_RENDERER, so no need to delete outside the ifdef |
| 40 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | 1365674 | 2013-08-19 17:42:51 -0700 | [diff] [blame] | 41 | if (f && android::uirenderer::Caches::hasInstance()) { |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 42 | android::uirenderer::Caches::getInstance().resourceCache.destructor(f); |
| 43 | } else { |
| 44 | delete f; |
| 45 | } |
| 46 | #endif |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 47 | } |
| 48 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 49 | static jlong glCreatePorterDuffFilter(JNIEnv* env, jobject, jlong skFilterHandle, |
| 50 | jint srcColor, jint modeHandle) { |
| 51 | SkColorFilter *skFilter = reinterpret_cast<SkColorFilter *>(skFilterHandle); |
| 52 | SkPorterDuff::Mode mode = static_cast<SkPorterDuff::Mode>(modeHandle); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 53 | #ifdef USE_OPENGL_RENDERER |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 54 | return reinterpret_cast<jlong>(new SkiaBlendFilter(skFilter, srcColor, SkPorterDuff::ToXfermodeMode(mode))); |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 55 | #else |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 56 | return NULL; |
| 57 | #endif |
| 58 | } |
| 59 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 60 | static jlong glCreateLightingFilter(JNIEnv* env, jobject, jlong skFilterHandle, |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 61 | jint mul, jint add) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 62 | SkColorFilter *skFilter = reinterpret_cast<SkColorFilter *>(skFilterHandle); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 63 | #ifdef USE_OPENGL_RENDERER |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 64 | return reinterpret_cast<jlong>(new SkiaLightingFilter(skFilter, mul, add)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 65 | #else |
| 66 | return NULL; |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 67 | #endif |
| 68 | } |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 69 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 70 | static jlong glCreateColorMatrixFilter(JNIEnv* env, jobject, jlong skFilterHandle, |
Chet Haase | ad93c2b | 2010-10-22 16:17:12 -0700 | [diff] [blame] | 71 | jfloatArray jarray) { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 72 | SkColorFilter *skFilter = reinterpret_cast<SkColorFilter *>(skFilterHandle); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 73 | #ifdef USE_OPENGL_RENDERER |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 74 | AutoJavaFloatArray autoArray(env, jarray, 20); |
| 75 | const float* src = autoArray.ptr(); |
| 76 | |
| 77 | float* colorMatrix = new float[16]; |
| 78 | memcpy(colorMatrix, src, 4 * sizeof(float)); |
| 79 | memcpy(&colorMatrix[4], &src[5], 4 * sizeof(float)); |
| 80 | memcpy(&colorMatrix[8], &src[10], 4 * sizeof(float)); |
| 81 | memcpy(&colorMatrix[12], &src[15], 4 * sizeof(float)); |
| 82 | |
| 83 | float* colorVector = new float[4]; |
| 84 | colorVector[0] = src[4]; |
| 85 | colorVector[1] = src[9]; |
| 86 | colorVector[2] = src[14]; |
| 87 | colorVector[3] = src[19]; |
| 88 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 89 | return reinterpret_cast<jlong>(new SkiaColorMatrixFilter(skFilter, colorMatrix, colorVector)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 90 | #else |
| 91 | return NULL; |
| 92 | #endif |
| 93 | } |
| 94 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 95 | static jlong CreatePorterDuffFilter(JNIEnv* env, jobject, jint srcColor, |
| 96 | jint modeHandle) { |
| 97 | SkPorterDuff::Mode mode = (SkPorterDuff::Mode) modeHandle; |
| 98 | return reinterpret_cast<jlong>(SkColorFilter::CreateModeFilter(srcColor, SkPorterDuff::ToXfermodeMode(mode))); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 99 | } |
| 100 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 101 | static jlong CreateLightingFilter(JNIEnv* env, jobject, jint mul, jint add) { |
| 102 | return reinterpret_cast<jlong>(SkColorFilter::CreateLightingFilter(mul, add)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 103 | } |
| 104 | |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 105 | static jlong CreateColorMatrixFilter(JNIEnv* env, jobject, jfloatArray jarray) { |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 106 | AutoJavaFloatArray autoArray(env, jarray, 20); |
| 107 | const float* src = autoArray.ptr(); |
| 108 | |
| 109 | #ifdef SK_SCALAR_IS_FIXED |
| 110 | SkFixed array[20]; |
| 111 | for (int i = 0; i < 20; i++) { |
| 112 | array[i] = SkFloatToScalar(src[i]); |
| 113 | } |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 114 | return reinterpret_cast<jlong>(new SkColorMatrixFilter(array)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 115 | #else |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 116 | return reinterpret_cast<jlong>(new SkColorMatrixFilter(src)); |
Romain Guy | 1639351 | 2010-08-08 00:14:31 -0700 | [diff] [blame] | 117 | #endif |
Romain Guy | db1938e | 2010-08-02 18:50:22 -0700 | [diff] [blame] | 118 | } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 119 | }; |
| 120 | |
| 121 | static JNINativeMethod colorfilter_methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 122 | {"destroyFilter", "(JJ)V", (void*) SkColorFilterGlue::finalizer} |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 123 | }; |
| 124 | |
| 125 | static JNINativeMethod porterduff_methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 126 | { "native_CreatePorterDuffFilter", "(II)J", (void*) SkColorFilterGlue::CreatePorterDuffFilter }, |
| 127 | { "nCreatePorterDuffFilter", "(JII)J", (void*) SkColorFilterGlue::glCreatePorterDuffFilter } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 128 | }; |
| 129 | |
| 130 | static JNINativeMethod lighting_methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 131 | { "native_CreateLightingFilter", "(II)J", (void*) SkColorFilterGlue::CreateLightingFilter }, |
| 132 | { "nCreateLightingFilter", "(JII)J", (void*) SkColorFilterGlue::glCreateLightingFilter }, |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 133 | }; |
| 134 | |
| 135 | static JNINativeMethod colormatrix_methods[] = { |
Ashok Bhat | 36bef0b | 2014-01-20 20:08:01 +0000 | [diff] [blame] | 136 | { "nativeColorMatrixFilter", "([F)J", (void*) SkColorFilterGlue::CreateColorMatrixFilter }, |
| 137 | { "nColorMatrixFilter", "(J[F)J", (void*) SkColorFilterGlue::glCreateColorMatrixFilter } |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 138 | }; |
| 139 | |
| 140 | #define REG(env, name, array) \ |
| 141 | result = android::AndroidRuntime::registerNativeMethods(env, name, array, \ |
| 142 | SK_ARRAY_COUNT(array)); \ |
| 143 | if (result < 0) return result |
| 144 | |
The Android Open Source Project | 9066cfe | 2009-03-03 19:31:44 -0800 | [diff] [blame] | 145 | int register_android_graphics_ColorFilter(JNIEnv* env) { |
| 146 | int result; |
| 147 | |
| 148 | REG(env, "android/graphics/ColorFilter", colorfilter_methods); |
| 149 | REG(env, "android/graphics/PorterDuffColorFilter", porterduff_methods); |
| 150 | REG(env, "android/graphics/LightingColorFilter", lighting_methods); |
| 151 | REG(env, "android/graphics/ColorMatrixColorFilter", colormatrix_methods); |
| 152 | |
| 153 | return 0; |
| 154 | } |
| 155 | |
| 156 | } |