1 /*
2  * Copyright (C) 2019 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 "android/graphics/jni_runtime.h"
18 
19 #include <EGL/egl.h>
20 #include <GraphicsJNI.h>
21 #include <Properties.h>
22 #include <SkGraphics.h>
23 #include <android/log.h>
24 #include <nativehelper/JNIHelp.h>
25 #include <sys/cdefs.h>
26 #include <vulkan/vulkan.h>
27 
28 #undef LOG_TAG
29 #define LOG_TAG "AndroidGraphicsJNI"
30 
31 extern int register_android_graphics_Bitmap(JNIEnv*);
32 extern int register_android_graphics_BitmapFactory(JNIEnv*);
33 extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
34 extern int register_android_graphics_ByteBufferStreamAdaptor(JNIEnv* env);
35 extern int register_android_graphics_Camera(JNIEnv* env);
36 extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
37 extern int register_android_graphics_Graphics(JNIEnv* env);
38 extern int register_android_graphics_ImageDecoder(JNIEnv*);
39 extern int register_android_graphics_drawable_AnimatedImageDrawable(JNIEnv*);
40 extern int register_android_graphics_Interpolator(JNIEnv* env);
41 extern int register_android_graphics_MaskFilter(JNIEnv* env);
42 extern int register_android_graphics_Movie(JNIEnv* env);
43 extern int register_android_graphics_NinePatch(JNIEnv*);
44 extern int register_android_graphics_PathEffect(JNIEnv* env);
45 extern int register_android_graphics_Shader(JNIEnv* env);
46 extern int register_android_graphics_RenderEffect(JNIEnv* env);
47 extern int register_android_graphics_Typeface(JNIEnv* env);
48 extern int register_android_graphics_YuvImage(JNIEnv* env);
49 
50 namespace android {
51 
52 extern int register_android_graphics_Canvas(JNIEnv* env);
53 extern int register_android_graphics_CanvasProperty(JNIEnv* env);
54 extern int register_android_graphics_ColorFilter(JNIEnv* env);
55 extern int register_android_graphics_ColorSpace(JNIEnv* env);
56 extern int register_android_graphics_DrawFilter(JNIEnv* env);
57 extern int register_android_graphics_FontFamily(JNIEnv* env);
58 extern int register_android_graphics_Gainmap(JNIEnv* env);
59 extern int register_android_graphics_HardwareRendererObserver(JNIEnv* env);
60 extern int register_android_graphics_Matrix(JNIEnv* env);
61 extern int register_android_graphics_Paint(JNIEnv* env);
62 extern int register_android_graphics_Path(JNIEnv* env);
63 extern int register_android_graphics_PathIterator(JNIEnv* env);
64 extern int register_android_graphics_PathMeasure(JNIEnv* env);
65 extern int register_android_graphics_Picture(JNIEnv*);
66 extern int register_android_graphics_Region(JNIEnv* env);
67 extern int register_android_graphics_TextureLayer(JNIEnv* env);
68 extern int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env);
69 extern int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env);
70 extern int register_android_graphics_drawable_AnimatedVectorDrawable(JNIEnv* env);
71 extern int register_android_graphics_drawable_VectorDrawable(JNIEnv* env);
72 extern int register_android_graphics_fonts_Font(JNIEnv* env);
73 extern int register_android_graphics_fonts_FontFamily(JNIEnv* env);
74 extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
75 extern int register_android_graphics_pdf_PdfEditor(JNIEnv* env);
76 extern int register_android_graphics_pdf_PdfRenderer(JNIEnv* env);
77 extern int register_android_graphics_text_MeasuredText(JNIEnv* env);
78 extern int register_android_graphics_text_LineBreaker(JNIEnv *env);
79 extern int register_android_graphics_text_TextShaper(JNIEnv *env);
80 extern int register_android_graphics_text_GraphemeBreak(JNIEnv* env);
81 extern int register_android_graphics_MeshSpecification(JNIEnv* env);
82 extern int register_android_graphics_Mesh(JNIEnv* env);
83 
84 extern int register_android_util_PathParser(JNIEnv* env);
85 extern int register_android_view_DisplayListCanvas(JNIEnv* env);
86 extern int register_android_view_RenderNode(JNIEnv* env);
87 extern int register_android_view_ThreadedRenderer(JNIEnv* env);
88 extern int register_android_graphics_HardwareBufferRenderer(JNIEnv* env);
89 
90 #ifdef NDEBUG
91     #define REG_JNI(name)      { name }
92     struct RegJNIRec {
93         int (*mProc)(JNIEnv*);
94     };
95 #else
96     #define REG_JNI(name)      { name, #name }
97     struct RegJNIRec {
98         int (*mProc)(JNIEnv*);
99         const char* mName;
100     };
101 #endif
102 
103     static const RegJNIRec gRegJNI[] = {
104             REG_JNI(register_android_graphics_Canvas),
105             // This needs to be before register_android_graphics_Graphics, or the latter
106             // will not be able to find the jmethodID for ColorSpace.get().
107             REG_JNI(register_android_graphics_ColorSpace),
108             REG_JNI(register_android_graphics_Graphics),
109             REG_JNI(register_android_graphics_Bitmap),
110             REG_JNI(register_android_graphics_BitmapFactory),
111             REG_JNI(register_android_graphics_BitmapRegionDecoder),
112             REG_JNI(register_android_graphics_ByteBufferStreamAdaptor),
113             REG_JNI(register_android_graphics_Camera),
114             REG_JNI(register_android_graphics_CreateJavaOutputStreamAdaptor),
115             REG_JNI(register_android_graphics_CanvasProperty),
116             REG_JNI(register_android_graphics_ColorFilter),
117             REG_JNI(register_android_graphics_DrawFilter),
118             REG_JNI(register_android_graphics_FontFamily),
119             REG_JNI(register_android_graphics_Gainmap),
120             REG_JNI(register_android_graphics_HardwareRendererObserver),
121             REG_JNI(register_android_graphics_ImageDecoder),
122             REG_JNI(register_android_graphics_drawable_AnimatedImageDrawable),
123             REG_JNI(register_android_graphics_Interpolator),
124             REG_JNI(register_android_graphics_MaskFilter),
125             REG_JNI(register_android_graphics_Matrix),
126             REG_JNI(register_android_graphics_Movie),
127             REG_JNI(register_android_graphics_NinePatch),
128             REG_JNI(register_android_graphics_Paint),
129             REG_JNI(register_android_graphics_Path),
130             REG_JNI(register_android_graphics_PathIterator),
131             REG_JNI(register_android_graphics_PathMeasure),
132             REG_JNI(register_android_graphics_PathEffect),
133             REG_JNI(register_android_graphics_Picture),
134             REG_JNI(register_android_graphics_Region),
135             REG_JNI(register_android_graphics_Shader),
136             REG_JNI(register_android_graphics_RenderEffect),
137             REG_JNI(register_android_graphics_TextureLayer),
138             REG_JNI(register_android_graphics_Typeface),
139             REG_JNI(register_android_graphics_YuvImage),
140             REG_JNI(register_android_graphics_animation_NativeInterpolatorFactory),
141             REG_JNI(register_android_graphics_animation_RenderNodeAnimator),
142             REG_JNI(register_android_graphics_drawable_AnimatedVectorDrawable),
143             REG_JNI(register_android_graphics_drawable_VectorDrawable),
144             REG_JNI(register_android_graphics_fonts_Font),
145             REG_JNI(register_android_graphics_fonts_FontFamily),
146             REG_JNI(register_android_graphics_pdf_PdfDocument),
147             REG_JNI(register_android_graphics_pdf_PdfEditor),
148             REG_JNI(register_android_graphics_pdf_PdfRenderer),
149             REG_JNI(register_android_graphics_text_MeasuredText),
150             REG_JNI(register_android_graphics_text_LineBreaker),
151             REG_JNI(register_android_graphics_text_TextShaper),
152             REG_JNI(register_android_graphics_text_GraphemeBreak),
153             REG_JNI(register_android_graphics_MeshSpecification),
154             REG_JNI(register_android_graphics_Mesh),
155 
156             REG_JNI(register_android_util_PathParser),
157             REG_JNI(register_android_view_RenderNode),
158             REG_JNI(register_android_view_DisplayListCanvas),
159             REG_JNI(register_android_graphics_HardwareBufferRenderer),
160 
161             REG_JNI(register_android_view_ThreadedRenderer),
162     };
163 
164 } // namespace android
165 
init_android_graphics()166 void init_android_graphics() {
167     SkGraphics::Init();
168 }
169 
register_android_graphics_classes(JNIEnv * env)170 int register_android_graphics_classes(JNIEnv *env) {
171     JavaVM* vm = nullptr;
172     env->GetJavaVM(&vm);
173     GraphicsJNI::setJavaVM(vm);
174 
175     for (size_t i = 0; i < NELEM(android::gRegJNI); i++) {
176         if (android::gRegJNI[i].mProc(env) < 0) {
177 #ifndef NDEBUG
178             __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "JNI Error!!! %s failed to load\n",
179                                 android::gRegJNI[i].mName);
180 #endif
181             return -1;
182         }
183     }
184     return 0;
185 }
186 
187 using android::uirenderer::Properties;
188 using android::uirenderer::RenderPipelineType;
189 
zygote_preload_graphics()190 void zygote_preload_graphics() {
191     if (Properties::peekRenderPipelineType() == RenderPipelineType::SkiaGL) {
192         // Preload GL driver if HWUI renders with GL backend.
193         eglGetDisplay(EGL_DEFAULT_DISPLAY);
194     } else {
195         // Preload Vulkan driver if HWUI renders with Vulkan backend.
196         uint32_t apiVersion;
197         vkEnumerateInstanceVersion(&apiVersion);
198     }
199 }
200