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