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