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