Derek Sollenberger | c5882c4 | 2019-10-25 11:11:32 -0400 | [diff] [blame^] | 1 | /* |
| 2 | * Copyright (C) 2020 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | */ |
| 16 | |
| 17 | #include "graphics_jni_helpers.h" |
| 18 | |
| 19 | #include <GraphicsJNI.h> |
| 20 | #include <SkGraphics.h> |
| 21 | |
| 22 | #include <sstream> |
| 23 | #include <iostream> |
| 24 | #include <unicode/putil.h> |
| 25 | #include <unordered_map> |
| 26 | #include <vector> |
| 27 | |
| 28 | using namespace std; |
| 29 | |
| 30 | /* |
| 31 | * This is responsible for setting up the JNI environment for communication between |
| 32 | * the Java and native parts of layoutlib, including registering native methods. |
| 33 | * This is mostly achieved by copying the way it is done in the platform |
| 34 | * (see AndroidRuntime.cpp). |
| 35 | */ |
| 36 | |
| 37 | static JavaVM* javaVM; |
| 38 | |
| 39 | extern int register_android_graphics_Bitmap(JNIEnv*); |
| 40 | extern int register_android_graphics_BitmapFactory(JNIEnv*); |
| 41 | extern int register_android_graphics_ByteBufferStreamAdaptor(JNIEnv* env); |
| 42 | extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env); |
| 43 | extern int register_android_graphics_Graphics(JNIEnv* env); |
| 44 | extern int register_android_graphics_ImageDecoder(JNIEnv*); |
| 45 | extern int register_android_graphics_Interpolator(JNIEnv* env); |
| 46 | extern int register_android_graphics_MaskFilter(JNIEnv* env); |
| 47 | extern int register_android_graphics_NinePatch(JNIEnv*); |
| 48 | extern int register_android_graphics_PathEffect(JNIEnv* env); |
| 49 | extern int register_android_graphics_Shader(JNIEnv* env); |
| 50 | extern int register_android_graphics_Typeface(JNIEnv* env); |
| 51 | |
| 52 | namespace android { |
| 53 | |
| 54 | extern int register_android_graphics_Canvas(JNIEnv* env); |
| 55 | extern int register_android_graphics_ColorFilter(JNIEnv* env); |
| 56 | extern int register_android_graphics_ColorSpace(JNIEnv* env); |
| 57 | extern int register_android_graphics_DrawFilter(JNIEnv* env); |
| 58 | extern int register_android_graphics_FontFamily(JNIEnv* env); |
| 59 | extern int register_android_graphics_Matrix(JNIEnv* env); |
| 60 | extern int register_android_graphics_Paint(JNIEnv* env); |
| 61 | extern int register_android_graphics_Path(JNIEnv* env); |
| 62 | extern int register_android_graphics_PathMeasure(JNIEnv* env); |
| 63 | extern int register_android_graphics_Picture(JNIEnv* env); |
| 64 | //extern int register_android_graphics_Region(JNIEnv* env); |
| 65 | extern int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env); |
| 66 | extern int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env); |
| 67 | extern int register_android_graphics_drawable_AnimatedVectorDrawable(JNIEnv* env); |
| 68 | extern int register_android_graphics_drawable_VectorDrawable(JNIEnv* env); |
| 69 | extern int register_android_graphics_fonts_Font(JNIEnv* env); |
| 70 | extern int register_android_graphics_fonts_FontFamily(JNIEnv* env); |
| 71 | extern int register_android_graphics_text_LineBreaker(JNIEnv* env); |
| 72 | extern int register_android_graphics_text_MeasuredText(JNIEnv* env); |
| 73 | extern int register_android_util_PathParser(JNIEnv* env); |
| 74 | extern int register_android_view_RenderNode(JNIEnv* env); |
| 75 | extern int register_android_view_DisplayListCanvas(JNIEnv* env); |
| 76 | |
| 77 | #define REG_JNI(name) { name } |
| 78 | struct RegJNIRec { |
| 79 | int (*mProc)(JNIEnv*); |
| 80 | }; |
| 81 | |
| 82 | // Map of all possible class names to register to their corresponding JNI registration function pointer |
| 83 | // The actual list of registered classes will be determined at runtime via the 'native_classes' System property |
| 84 | static const std::unordered_map<std::string, RegJNIRec> gRegJNIMap = { |
| 85 | {"android.graphics.Bitmap", REG_JNI(register_android_graphics_Bitmap)}, |
| 86 | {"android.graphics.BitmapFactory", REG_JNI(register_android_graphics_BitmapFactory)}, |
| 87 | {"android.graphics.ByteBufferStreamAdaptor", |
| 88 | REG_JNI(register_android_graphics_ByteBufferStreamAdaptor)}, |
| 89 | {"android.graphics.Canvas", REG_JNI(register_android_graphics_Canvas)}, |
| 90 | {"android.graphics.RenderNode", REG_JNI(register_android_view_RenderNode)}, |
| 91 | {"android.graphics.ColorFilter", REG_JNI(register_android_graphics_ColorFilter)}, |
| 92 | {"android.graphics.ColorSpace", REG_JNI(register_android_graphics_ColorSpace)}, |
| 93 | {"android.graphics.CreateJavaOutputStreamAdaptor", |
| 94 | REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor)}, |
| 95 | {"android.graphics.DrawFilter", REG_JNI(register_android_graphics_DrawFilter)}, |
| 96 | {"android.graphics.FontFamily", REG_JNI(register_android_graphics_FontFamily)}, |
| 97 | {"android.graphics.Graphics", REG_JNI(register_android_graphics_Graphics)}, |
| 98 | {"android.graphics.ImageDecoder", REG_JNI(register_android_graphics_ImageDecoder)}, |
| 99 | {"android.graphics.Interpolator", REG_JNI(register_android_graphics_Interpolator)}, |
| 100 | {"android.graphics.MaskFilter", REG_JNI(register_android_graphics_MaskFilter)}, |
| 101 | {"android.graphics.Matrix", REG_JNI(register_android_graphics_Matrix)}, |
| 102 | {"android.graphics.NinePatch", REG_JNI(register_android_graphics_NinePatch)}, |
| 103 | {"android.graphics.Paint", REG_JNI(register_android_graphics_Paint)}, |
| 104 | {"android.graphics.Path", REG_JNI(register_android_graphics_Path)}, |
| 105 | {"android.graphics.PathEffect", REG_JNI(register_android_graphics_PathEffect)}, |
| 106 | {"android.graphics.PathMeasure", REG_JNI(register_android_graphics_PathMeasure)}, |
| 107 | {"android.graphics.Picture", REG_JNI(register_android_graphics_Picture)}, |
| 108 | {"android.graphics.RecordingCanvas", REG_JNI(register_android_view_DisplayListCanvas)}, |
| 109 | // {"android.graphics.Region", REG_JNI(register_android_graphics_Region)}, |
| 110 | {"android.graphics.Shader", REG_JNI(register_android_graphics_Shader)}, |
| 111 | {"android.graphics.Typeface", REG_JNI(register_android_graphics_Typeface)}, |
| 112 | {"android.graphics.animation.NativeInterpolatorFactory", |
| 113 | REG_JNI(register_android_graphics_animation_NativeInterpolatorFactory)}, |
| 114 | {"android.graphics.animation.RenderNodeAnimator", |
| 115 | REG_JNI(register_android_graphics_animation_RenderNodeAnimator)}, |
| 116 | {"android.graphics.drawable.AnimatedVectorDrawable", |
| 117 | REG_JNI(register_android_graphics_drawable_AnimatedVectorDrawable)}, |
| 118 | {"android.graphics.drawable.VectorDrawable", |
| 119 | REG_JNI(register_android_graphics_drawable_VectorDrawable)}, |
| 120 | {"android.graphics.fonts.Font", REG_JNI(register_android_graphics_fonts_Font)}, |
| 121 | {"android.graphics.fonts.FontFamily", REG_JNI(register_android_graphics_fonts_FontFamily)}, |
| 122 | {"android.graphics.text.LineBreaker", REG_JNI(register_android_graphics_text_LineBreaker)}, |
| 123 | {"android.graphics.text.MeasuredText", |
| 124 | REG_JNI(register_android_graphics_text_MeasuredText)}, |
| 125 | {"android.util.PathParser", REG_JNI(register_android_util_PathParser)}, |
| 126 | }; |
| 127 | |
| 128 | static int register_jni_procs(const std::unordered_map<std::string, RegJNIRec>& jniRegMap, |
| 129 | const vector<string>& classesToRegister, JNIEnv* env) { |
| 130 | |
| 131 | for (const string& className : classesToRegister) { |
| 132 | if (jniRegMap.at(className).mProc(env) < 0) { |
| 133 | return -1; |
| 134 | } |
| 135 | } |
| 136 | return 0; |
| 137 | } |
| 138 | |
| 139 | static vector<string> parseCsv(const string& csvString) { |
| 140 | vector<string> result; |
| 141 | istringstream stream(csvString); |
| 142 | string segment; |
| 143 | while(getline(stream, segment, ',')) |
| 144 | { |
| 145 | result.push_back(segment); |
| 146 | } |
| 147 | return result; |
| 148 | } |
| 149 | |
| 150 | static vector<string> parseCsv(JNIEnv* env, jstring csvJString) { |
| 151 | const char* charArray = env->GetStringUTFChars(csvJString, 0); |
| 152 | string csvString(charArray); |
| 153 | vector<string> result = parseCsv(csvString); |
| 154 | env->ReleaseStringUTFChars(csvJString, charArray); |
| 155 | return result; |
| 156 | } |
| 157 | |
| 158 | } // namespace android |
| 159 | |
| 160 | using namespace android; |
| 161 | |
| 162 | void init_android_graphics() { |
| 163 | SkGraphics::Init(); |
| 164 | } |
| 165 | |
| 166 | int register_android_graphics_classes(JNIEnv *env) { |
| 167 | JavaVM* vm = nullptr; |
| 168 | env->GetJavaVM(&vm); |
| 169 | GraphicsJNI::setJavaVM(vm); |
| 170 | |
| 171 | // Configuration is stored as java System properties. |
| 172 | // Get a reference to System.getProperty |
| 173 | jclass system = FindClassOrDie(env, "java/lang/System"); |
| 174 | jmethodID getPropertyMethod = GetStaticMethodIDOrDie(env, system, "getProperty", |
| 175 | "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;"); |
| 176 | |
| 177 | // Get the names of classes that need to register their native methods |
| 178 | auto nativesClassesJString = |
| 179 | (jstring) env->CallStaticObjectMethod(system, |
| 180 | getPropertyMethod, env->NewStringUTF("native_classes"), |
| 181 | env->NewStringUTF("")); |
| 182 | vector<string> classesToRegister = parseCsv(env, nativesClassesJString); |
| 183 | |
| 184 | if (register_jni_procs(gRegJNIMap, classesToRegister, env) < 0) { |
| 185 | return JNI_ERR; |
| 186 | } |
| 187 | |
| 188 | return 0; |
| 189 | } |
| 190 | |
| 191 | void zygote_preload_graphics() { } |