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 "GraphicsJNI.h"
19 
20 #include "hwui/Paint.h"
21 #include "hwui/PaintFilter.h"
22 #include "SkPaint.h"
23 
24 namespace android {
25 
26 class PaintFlagsFilter : public PaintFilter {
27 public:
PaintFlagsFilter(uint32_t clearFlags,uint32_t setFlags)28     PaintFlagsFilter(uint32_t clearFlags, uint32_t setFlags) {
29         fClearFlags = static_cast<uint16_t>(clearFlags);
30         fSetFlags = static_cast<uint16_t>(setFlags);
31     }
filterFullPaint(Paint * paint)32     void filterFullPaint(Paint* paint) override {
33         paint->setJavaFlags((paint->getJavaFlags() & ~fClearFlags) | fSetFlags);
34     }
35 
36 private:
37     uint16_t fClearFlags;
38     uint16_t fSetFlags;
39 };
40 
41 class PaintFilterGlue {
42 public:
43 
finalizer(JNIEnv * env,jobject clazz,jlong objHandle)44     static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) {
45         PaintFilter* obj = reinterpret_cast<PaintFilter*>(objHandle);
46         SkSafeUnref(obj);
47     }
48 
CreatePaintFlagsFilter(JNIEnv * env,jobject clazz,jint clearFlags,jint setFlags)49     static jlong CreatePaintFlagsFilter(JNIEnv* env, jobject clazz,
50                                         jint clearFlags, jint setFlags) {
51         PaintFilter* filter = nullptr;
52         if (clearFlags | setFlags) {
53             filter = new PaintFlagsFilter(clearFlags, setFlags);
54         }
55         return reinterpret_cast<jlong>(filter);
56     }
57 };
58 
59 static const JNINativeMethod drawfilter_methods[] = {
60     {"nativeDestructor", "(J)V", (void*) PaintFilterGlue::finalizer}
61 };
62 
63 static const JNINativeMethod paintflags_methods[] = {
64     {"nativeConstructor","(II)J", (void*) PaintFilterGlue::CreatePaintFlagsFilter}
65 };
66 
register_android_graphics_DrawFilter(JNIEnv * env)67 int register_android_graphics_DrawFilter(JNIEnv* env) {
68     int result = RegisterMethodsOrDie(env, "android/graphics/DrawFilter", drawfilter_methods,
69                                       NELEM(drawfilter_methods));
70     result |= RegisterMethodsOrDie(env, "android/graphics/PaintFlagsDrawFilter", paintflags_methods,
71                                    NELEM(paintflags_methods));
72 
73     return result;
74 }
75 
76 }
77