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 <GraphicsJNI.h>
18 #include <SkGraphics.h>
19 
20 #include <unordered_map>
21 #include <vector>
22 
23 #include "Properties.h"
24 #include "android/graphics/jni_runtime.h"
25 #include "graphics_jni_helpers.h"
26 
27 using namespace std;
28 
29 extern int register_android_graphics_Bitmap(JNIEnv*);
30 extern int register_android_graphics_BitmapFactory(JNIEnv*);
31 extern int register_android_graphics_ByteBufferStreamAdaptor(JNIEnv* env);
32 extern int register_android_graphics_Camera(JNIEnv* env);
33 extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
34 extern int register_android_graphics_Graphics(JNIEnv* env);
35 extern int register_android_graphics_ImageDecoder(JNIEnv*);
36 extern int register_android_graphics_Interpolator(JNIEnv* env);
37 extern int register_android_graphics_MaskFilter(JNIEnv* env);
38 extern int register_android_graphics_NinePatch(JNIEnv*);
39 extern int register_android_graphics_PathEffect(JNIEnv* env);
40 extern int register_android_graphics_Shader(JNIEnv* env);
41 extern int register_android_graphics_RenderEffect(JNIEnv* env);
42 extern int register_android_graphics_Typeface(JNIEnv* env);
43 extern int register_android_graphics_YuvImage(JNIEnv* env);
44 
45 namespace android {
46 
47 extern int register_android_graphics_Canvas(JNIEnv* env);
48 extern int register_android_graphics_CanvasProperty(JNIEnv* env);
49 extern int register_android_graphics_Color(JNIEnv* env);
50 extern int register_android_graphics_ColorFilter(JNIEnv* env);
51 extern int register_android_graphics_ColorSpace(JNIEnv* env);
52 extern int register_android_graphics_DrawFilter(JNIEnv* env);
53 extern int register_android_graphics_FontFamily(JNIEnv* env);
54 extern int register_android_graphics_Matrix(JNIEnv* env);
55 extern int register_android_graphics_Paint(JNIEnv* env);
56 extern int register_android_graphics_Path(JNIEnv* env);
57 extern int register_android_graphics_PathIterator(JNIEnv* env);
58 extern int register_android_graphics_PathMeasure(JNIEnv* env);
59 extern int register_android_graphics_Picture(JNIEnv* env);
60 extern int register_android_graphics_Region(JNIEnv* env);
61 extern int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env);
62 extern int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env);
63 extern int register_android_graphics_drawable_AnimatedVectorDrawable(JNIEnv* env);
64 extern int register_android_graphics_drawable_VectorDrawable(JNIEnv* env);
65 extern int register_android_graphics_fonts_Font(JNIEnv* env);
66 extern int register_android_graphics_fonts_FontFamily(JNIEnv* env);
67 extern int register_android_graphics_text_LineBreaker(JNIEnv* env);
68 extern int register_android_graphics_text_MeasuredText(JNIEnv* env);
69 extern int register_android_graphics_text_TextShaper(JNIEnv* env);
70 extern int register_android_graphics_text_GraphemeBreak(JNIEnv* env);
71 
72 extern int register_android_util_PathParser(JNIEnv* env);
73 extern int register_android_view_DisplayListCanvas(JNIEnv* env);
74 extern int register_android_view_RenderNode(JNIEnv* env);
75 
76 #define REG_JNI(name)      { name }
77 struct RegJNIRec {
78     int (*mProc)(JNIEnv*);
79 };
80 
81 // Map of all possible class names to register to their corresponding JNI registration function pointer
82 // The actual list of registered classes will be determined at runtime via the 'native_classes' System property
83 static const std::unordered_map<std::string, RegJNIRec> gRegJNIMap = {
84         {"android.graphics.Bitmap", REG_JNI(register_android_graphics_Bitmap)},
85         {"android.graphics.BitmapFactory", REG_JNI(register_android_graphics_BitmapFactory)},
86         {"android.graphics.ByteBufferStreamAdaptor",
87          REG_JNI(register_android_graphics_ByteBufferStreamAdaptor)},
88         {"android.graphics.Camera", REG_JNI(register_android_graphics_Camera)},
89         {"android.graphics.Canvas", REG_JNI(register_android_graphics_Canvas)},
90         {"android.graphics.CanvasProperty", REG_JNI(register_android_graphics_CanvasProperty)},
91         {"android.graphics.Color", REG_JNI(register_android_graphics_Color)},
92         {"android.graphics.ColorFilter", REG_JNI(register_android_graphics_ColorFilter)},
93         {"android.graphics.ColorSpace", REG_JNI(register_android_graphics_ColorSpace)},
94         {"android.graphics.CreateJavaOutputStreamAdaptor",
95          REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor)},
96         {"android.graphics.DrawFilter", REG_JNI(register_android_graphics_DrawFilter)},
97         {"android.graphics.FontFamily", REG_JNI(register_android_graphics_FontFamily)},
98         {"android.graphics.Graphics", REG_JNI(register_android_graphics_Graphics)},
99         {"android.graphics.ImageDecoder", REG_JNI(register_android_graphics_ImageDecoder)},
100         {"android.graphics.Interpolator", REG_JNI(register_android_graphics_Interpolator)},
101         {"android.graphics.MaskFilter", REG_JNI(register_android_graphics_MaskFilter)},
102         {"android.graphics.Matrix", REG_JNI(register_android_graphics_Matrix)},
103         {"android.graphics.NinePatch", REG_JNI(register_android_graphics_NinePatch)},
104         {"android.graphics.Paint", REG_JNI(register_android_graphics_Paint)},
105         {"android.graphics.Path", REG_JNI(register_android_graphics_Path)},
106         {"android.graphics.PathEffect", REG_JNI(register_android_graphics_PathEffect)},
107         {"android.graphics.PathIterator", REG_JNI(register_android_graphics_PathIterator)},
108         {"android.graphics.PathMeasure", REG_JNI(register_android_graphics_PathMeasure)},
109         {"android.graphics.Picture", REG_JNI(register_android_graphics_Picture)},
110         {"android.graphics.RecordingCanvas", REG_JNI(register_android_view_DisplayListCanvas)},
111         {"android.graphics.Region", REG_JNI(register_android_graphics_Region)},
112         {"android.graphics.RenderNode", REG_JNI(register_android_view_RenderNode)},
113         {"android.graphics.Shader", REG_JNI(register_android_graphics_Shader)},
114         {"android.graphics.RenderEffect", REG_JNI(register_android_graphics_RenderEffect)},
115         {"android.graphics.Typeface", REG_JNI(register_android_graphics_Typeface)},
116         {"android.graphics.YuvImage", REG_JNI(register_android_graphics_YuvImage)},
117         {"android.graphics.animation.NativeInterpolatorFactory",
118          REG_JNI(register_android_graphics_animation_NativeInterpolatorFactory)},
119         {"android.graphics.animation.RenderNodeAnimator",
120          REG_JNI(register_android_graphics_animation_RenderNodeAnimator)},
121         {"android.graphics.drawable.AnimatedVectorDrawable",
122          REG_JNI(register_android_graphics_drawable_AnimatedVectorDrawable)},
123         {"android.graphics.drawable.VectorDrawable",
124          REG_JNI(register_android_graphics_drawable_VectorDrawable)},
125         {"android.graphics.fonts.Font", REG_JNI(register_android_graphics_fonts_Font)},
126         {"android.graphics.fonts.FontFamily", REG_JNI(register_android_graphics_fonts_FontFamily)},
127         {"android.graphics.text.LineBreaker", REG_JNI(register_android_graphics_text_LineBreaker)},
128         {"android.graphics.text.MeasuredText",
129          REG_JNI(register_android_graphics_text_MeasuredText)},
130         {"android.graphics.text.TextRunShaper", REG_JNI(register_android_graphics_text_TextShaper)},
131         {"android.graphics.text.GraphemeBreak",
132          REG_JNI(register_android_graphics_text_GraphemeBreak)},
133         {"android.util.PathParser", REG_JNI(register_android_util_PathParser)},
134 };
135 
register_jni_procs(const std::unordered_map<std::string,RegJNIRec> & jniRegMap,const vector<string> & classesToRegister,JNIEnv * env)136 static int register_jni_procs(const std::unordered_map<std::string, RegJNIRec>& jniRegMap,
137         const vector<string>& classesToRegister, JNIEnv* env) {
138 
139     for (const string& className : classesToRegister) {
140         if (jniRegMap.at(className).mProc(env) < 0) {
141             return -1;
142         }
143     }
144     return 0;
145 }
146 
parseCsv(const string & csvString)147 static vector<string> parseCsv(const string& csvString) {
148     vector<string>   result;
149     istringstream stream(csvString);
150     string segment;
151     while(getline(stream, segment, ','))
152     {
153         result.push_back(segment);
154     }
155     return result;
156 }
157 
parseCsv(JNIEnv * env,jstring csvJString)158 static vector<string> parseCsv(JNIEnv* env, jstring csvJString) {
159     const char* charArray = env->GetStringUTFChars(csvJString, 0);
160     string csvString(charArray);
161     vector<string> result = parseCsv(csvString);
162     env->ReleaseStringUTFChars(csvJString, charArray);
163     return result;
164 }
165 
166 } // namespace android
167 
168 using namespace android;
169 using namespace android::uirenderer;
170 
init_android_graphics()171 void init_android_graphics() {
172     Properties::overrideRenderPipelineType(RenderPipelineType::SkiaCpu);
173     SkGraphics::Init();
174 }
175 
register_android_graphics_classes(JNIEnv * env)176 int register_android_graphics_classes(JNIEnv *env) {
177     JavaVM* vm = nullptr;
178     env->GetJavaVM(&vm);
179     GraphicsJNI::setJavaVM(vm);
180 
181     // Configuration is stored as java System properties.
182     // Get a reference to System.getProperty
183     jclass system = FindClassOrDie(env, "java/lang/System");
184     jmethodID getPropertyMethod = GetStaticMethodIDOrDie(env, system, "getProperty",
185                                                          "(Ljava/lang/String;Ljava/lang/String;)Ljava/lang/String;");
186 
187     // Get the names of classes that need to register their native methods
188     auto nativesClassesJString = (jstring)env->CallStaticObjectMethod(
189             system, getPropertyMethod, env->NewStringUTF("graphics_native_classes"),
190             env->NewStringUTF(""));
191     vector<string> classesToRegister = parseCsv(env, nativesClassesJString);
192 
193     if (register_jni_procs(gRegJNIMap, classesToRegister, env) < 0) {
194         return JNI_ERR;
195     }
196 
197     return 0;
198 }
199 
zygote_preload_graphics()200 void zygote_preload_graphics() { }
201