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 "core_jni_helpers.h"
28
29#include "SkDrawFilter.h"
30#include "SkPaintFlagsDrawFilter.h"
31#include "SkPaint.h"
32
33namespace android {
34
35// Custom version of SkPaintFlagsDrawFilter that also calls setFilterQuality.
36class CompatFlagsDrawFilter : public SkPaintFlagsDrawFilter {
37public:
38    CompatFlagsDrawFilter(uint32_t clearFlags, uint32_t setFlags,
39            SkFilterQuality desiredQuality)
40    : SkPaintFlagsDrawFilter(clearFlags, setFlags)
41    , fDesiredQuality(desiredQuality) {
42    }
43
44    virtual bool filter(SkPaint* paint, Type type) {
45        SkPaintFlagsDrawFilter::filter(paint, type);
46        paint->setFilterQuality(fDesiredQuality);
47        return true;
48    }
49
50private:
51    const SkFilterQuality fDesiredQuality;
52};
53
54// Returns whether flags contains FILTER_BITMAP_FLAG. If flags does, remove it.
55static inline bool hadFiltering(jint& flags) {
56    // Equivalent to the Java Paint's FILTER_BITMAP_FLAG.
57    static const uint32_t sFilterBitmapFlag = 0x02;
58
59    const bool result = (flags & sFilterBitmapFlag) != 0;
60    flags &= ~sFilterBitmapFlag;
61    return result;
62}
63
64class SkDrawFilterGlue {
65public:
66
67    static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) {
68        SkDrawFilter* obj = reinterpret_cast<SkDrawFilter*>(objHandle);
69        SkSafeUnref(obj);
70    }
71
72    static jlong CreatePaintFlagsDF(JNIEnv* env, jobject clazz,
73                                    jint clearFlags, jint setFlags) {
74        if (clearFlags | setFlags) {
75            // Mask both groups of flags to remove FILTER_BITMAP_FLAG, which no
76            // longer has a Skia equivalent flag (instead it corresponds to
77            // calling setFilterQuality), and keep track of which group(s), if
78            // any, had the flag set.
79            const bool turnFilteringOn = hadFiltering(setFlags);
80            const bool turnFilteringOff = hadFiltering(clearFlags);
81
82            SkDrawFilter* filter;
83            if (turnFilteringOn) {
84                // Turning filtering on overrides turning it off.
85                filter = new CompatFlagsDrawFilter(clearFlags, setFlags,
86                        kLow_SkFilterQuality);
87            } else if (turnFilteringOff) {
88                filter = new CompatFlagsDrawFilter(clearFlags, setFlags,
89                        kNone_SkFilterQuality);
90            } else {
91                filter = new SkPaintFlagsDrawFilter(clearFlags, setFlags);
92            }
93            return reinterpret_cast<jlong>(filter);
94        } else {
95            return NULL;
96        }
97    }
98};
99
100static const JNINativeMethod drawfilter_methods[] = {
101    {"nativeDestructor", "(J)V", (void*) SkDrawFilterGlue::finalizer}
102};
103
104static const JNINativeMethod paintflags_methods[] = {
105    {"nativeConstructor","(II)J", (void*) SkDrawFilterGlue::CreatePaintFlagsDF}
106};
107
108int register_android_graphics_DrawFilter(JNIEnv* env) {
109    int result = RegisterMethodsOrDie(env, "android/graphics/DrawFilter", drawfilter_methods,
110                                      NELEM(drawfilter_methods));
111    result |= RegisterMethodsOrDie(env, "android/graphics/PaintFlagsDrawFilter", paintflags_methods,
112                                   NELEM(paintflags_methods));
113
114    return 0;
115}
116
117}
118