DrawFilter.cpp revision 5f6aa983d620eb123cd11943235daa68f725ef46
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// This file was generated from the C++ include file: SkColorFilter.h
19// Any changes made to this file will be discarded by the build.
20// To change this file, either edit the include, or device/tools/gluemaker/main.cpp,
21// or one of the auxilary file specifications in device/tools/gluemaker.
22
23#include "jni.h"
24#include "GraphicsJNI.h"
25#include <android_runtime/AndroidRuntime.h>
26
27#include "SkDrawFilter.h"
28#include "SkPaintFlagsDrawFilter.h"
29#include "SkPaint.h"
30
31namespace android {
32
33// Custom version of SkPaintFlagsDrawFilter that also calls setFilterLevel.
34class CompatFlagsDrawFilter : public SkPaintFlagsDrawFilter {
35public:
36    CompatFlagsDrawFilter(uint32_t clearFlags, uint32_t setFlags,
37            SkPaint::FilterLevel desiredLevel)
38    : SkPaintFlagsDrawFilter(clearFlags, setFlags)
39    , fDesiredLevel(desiredLevel) {
40    }
41
42    virtual bool filter(SkPaint* paint, Type type) {
43        SkPaintFlagsDrawFilter::filter(paint, type);
44        paint->setFilterLevel(fDesiredLevel);
45        return true;
46    }
47
48private:
49    const SkPaint::FilterLevel fDesiredLevel;
50};
51
52// Returns whether flags contains FILTER_BITMAP_FLAG. If flags does, remove it.
53static inline bool hadFiltering(jint& flags) {
54    // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
55    static const uint32_t sFilterBitmapFlag = 0x02;
56
57    const bool result = (flags & sFilterBitmapFlag) != 0;
58    flags &= ~sFilterBitmapFlag;
59    return result;
60}
61
62class SkDrawFilterGlue {
63public:
64
65    static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) {
66        SkDrawFilter* obj = reinterpret_cast<SkDrawFilter*>(objHandle);
67        SkSafeUnref(obj);
68    }
69
70    static jlong CreatePaintFlagsDF(JNIEnv* env, jobject clazz,
71                                    jint clearFlags, jint setFlags) {
72        if (clearFlags | setFlags) {
73            // Mask both groups of flags to remove FILTER_BITMAP_FLAG, which no
74            // longer has a Skia equivalent flag (instead it corresponds to
75            // calling setFilterLevel), and keep track of which group(s), if
76            // any, had the flag set.
77            const bool turnFilteringOn = hadFiltering(setFlags);
78            const bool turnFilteringOff = hadFiltering(clearFlags);
79
80            SkDrawFilter* filter;
81            if (turnFilteringOn) {
82                // Turning filtering on overrides turning it off.
83                filter = new CompatFlagsDrawFilter(clearFlags, setFlags,
84                        SkPaint::kLow_FilterLevel);
85            } else if (turnFilteringOff) {
86                filter = new CompatFlagsDrawFilter(clearFlags, setFlags,
87                        SkPaint::kNone_FilterLevel);
88            } else {
89                filter = new SkPaintFlagsDrawFilter(clearFlags, setFlags);
90            }
91            return reinterpret_cast<jlong>(filter);
92        } else {
93            return NULL;
94        }
95    }
96};
97
98static JNINativeMethod drawfilter_methods[] = {
99    {"nativeDestructor", "(J)V", (void*) SkDrawFilterGlue::finalizer}
100};
101
102static JNINativeMethod paintflags_methods[] = {
103    {"nativeConstructor","(II)J", (void*) SkDrawFilterGlue::CreatePaintFlagsDF}
104};
105
106#define REG(env, name, array)                                                                       \
107    result = android::AndroidRuntime::registerNativeMethods(env, name, array, SK_ARRAY_COUNT(array));  \
108    if (result < 0) return result
109
110
111int register_android_graphics_DrawFilter(JNIEnv* env) {
112    int result;
113
114    REG(env, "android/graphics/DrawFilter", drawfilter_methods);
115    REG(env, "android/graphics/PaintFlagsDrawFilter", paintflags_methods);
116
117    return 0;
118}
119
120}
121