Home | History | Annotate | Download | only in graphics
      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 <SkiaColorFilter.h>
     27 #include <Caches.h>
     28 
     29 namespace android {
     30 
     31 using namespace uirenderer;
     32 
     33 class SkColorFilterGlue {
     34 public:
     35     static void finalizer(JNIEnv* env, jobject clazz, SkColorFilter* obj, SkiaColorFilter* f) {
     36         SkSafeUnref(obj);
     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
     45     }
     46 
     47     static SkiaColorFilter* glCreatePorterDuffFilter(JNIEnv* env, jobject, SkColorFilter *skFilter,
     48             jint srcColor, SkPorterDuff::Mode mode) {
     49 #ifdef USE_OPENGL_RENDERER
     50         return new SkiaBlendFilter(skFilter, srcColor, SkPorterDuff::ToXfermodeMode(mode));
     51 #else
     52         return NULL;
     53 #endif
     54     }
     55 
     56     static SkiaColorFilter* glCreateLightingFilter(JNIEnv* env, jobject, SkColorFilter *skFilter,
     57             jint mul, jint add) {
     58 #ifdef USE_OPENGL_RENDERER
     59         return new SkiaLightingFilter(skFilter, mul, add);
     60 #else
     61         return NULL;
     62 #endif
     63     }
     64 
     65     static SkiaColorFilter* glCreateColorMatrixFilter(JNIEnv* env, jobject, SkColorFilter *skFilter,
     66             jfloatArray jarray) {
     67 #ifdef USE_OPENGL_RENDERER
     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 
     83         return new SkiaColorMatrixFilter(skFilter, colorMatrix, colorVector);
     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
    111     }
    112 };
    113 
    114 static JNINativeMethod colorfilter_methods[] = {
    115     {"finalizer", "(II)V", (void*) SkColorFilterGlue::finalizer}
    116 };
    117 
    118 static JNINativeMethod porterduff_methods[] = {
    119     { "native_CreatePorterDuffFilter", "(II)I", (void*) SkColorFilterGlue::CreatePorterDuffFilter   },
    120     { "nCreatePorterDuffFilter",       "(III)I", (void*) SkColorFilterGlue::glCreatePorterDuffFilter }
    121 };
    122 
    123 static JNINativeMethod lighting_methods[] = {
    124     { "native_CreateLightingFilter", "(II)I", (void*) SkColorFilterGlue::CreateLightingFilter   },
    125     { "nCreateLightingFilter",       "(III)I", (void*) SkColorFilterGlue::glCreateLightingFilter },
    126 };
    127 
    128 static JNINativeMethod colormatrix_methods[] = {
    129     { "nativeColorMatrixFilter", "([F)I", (void*) SkColorFilterGlue::CreateColorMatrixFilter   },
    130     { "nColorMatrixFilter",      "(I[F)I", (void*) SkColorFilterGlue::glCreateColorMatrixFilter }
    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 
    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 }
    150