xref: /aosp_15_r20/frameworks/base/libs/hwui/apex/jni_runtime.cpp (revision d57664e9bc4670b3ecf6748a746a57c557b6bc9e)
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 extern int register_android_graphics_Bitmap(JNIEnv*);
29 extern int register_android_graphics_BitmapFactory(JNIEnv*);
30 extern int register_android_graphics_BitmapRegionDecoder(JNIEnv*);
31 extern int register_android_graphics_RuntimeXfermode(JNIEnv*);
32 extern int register_android_graphics_ByteBufferStreamAdaptor(JNIEnv* env);
33 extern int register_android_graphics_Camera(JNIEnv* env);
34 extern int register_android_graphics_CreateJavaOutputStreamAdaptor(JNIEnv* env);
35 extern int register_android_graphics_Graphics(JNIEnv* env);
36 extern int register_android_graphics_ImageDecoder(JNIEnv*);
37 extern int register_android_graphics_drawable_AnimatedImageDrawable(JNIEnv*);
38 extern int register_android_graphics_Interpolator(JNIEnv* env);
39 extern int register_android_graphics_MaskFilter(JNIEnv* env);
40 extern int register_android_graphics_Movie(JNIEnv* env);
41 extern int register_android_graphics_NinePatch(JNIEnv*);
42 extern int register_android_graphics_PathEffect(JNIEnv* env);
43 extern int register_android_graphics_Shader(JNIEnv* env);
44 extern int register_android_graphics_RenderEffect(JNIEnv* env);
45 extern int register_android_graphics_Typeface(JNIEnv* env);
46 extern int register_android_graphics_YuvImage(JNIEnv* env);
47 
48 namespace android {
49 
50 extern int register_android_graphics_Canvas(JNIEnv* env);
51 extern int register_android_graphics_CanvasProperty(JNIEnv* env);
52 extern int register_android_graphics_ColorFilter(JNIEnv* env);
53 extern int register_android_graphics_Color(JNIEnv* env);
54 extern int register_android_graphics_ColorSpace(JNIEnv* env);
55 extern int register_android_graphics_DrawFilter(JNIEnv* env);
56 extern int register_android_graphics_FontFamily(JNIEnv* env);
57 extern int register_android_graphics_Gainmap(JNIEnv* env);
58 extern int register_android_graphics_HardwareRendererObserver(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_PathIterator(JNIEnv* env);
63 extern int register_android_graphics_PathMeasure(JNIEnv* env);
64 extern int register_android_graphics_Picture(JNIEnv*);
65 extern int register_android_graphics_Region(JNIEnv* env);
66 extern int register_android_graphics_TextureLayer(JNIEnv* env);
67 extern int register_android_graphics_animation_NativeInterpolatorFactory(JNIEnv* env);
68 extern int register_android_graphics_animation_RenderNodeAnimator(JNIEnv* env);
69 extern int register_android_graphics_drawable_AnimatedVectorDrawable(JNIEnv* env);
70 extern int register_android_graphics_drawable_VectorDrawable(JNIEnv* env);
71 extern int register_android_graphics_fonts_Font(JNIEnv* env);
72 extern int register_android_graphics_fonts_FontFamily(JNIEnv* env);
73 extern int register_android_graphics_pdf_PdfDocument(JNIEnv* env);
74 extern int register_android_graphics_pdf_PdfEditor(JNIEnv* env);
75 extern int register_android_graphics_text_MeasuredText(JNIEnv* env);
76 extern int register_android_graphics_text_LineBreaker(JNIEnv *env);
77 extern int register_android_graphics_text_TextShaper(JNIEnv *env);
78 extern int register_android_graphics_text_GraphemeBreak(JNIEnv* env);
79 extern int register_android_graphics_MeshSpecification(JNIEnv* env);
80 extern int register_android_graphics_Mesh(JNIEnv* env);
81 
82 extern int register_android_util_PathParser(JNIEnv* env);
83 extern int register_android_view_DisplayListCanvas(JNIEnv* env);
84 extern int register_android_view_RenderNode(JNIEnv* env);
85 extern int register_android_view_ThreadedRenderer(JNIEnv* env);
86 extern int register_android_graphics_HardwareBufferRenderer(JNIEnv* env);
87 
88 #ifdef NDEBUG
89     #define REG_JNI(name)      { name }
90     struct RegJNIRec {
91         int (*mProc)(JNIEnv*);
92     };
93 #else
94     #define REG_JNI(name)      { name, #name }
95     struct RegJNIRec {
96         int (*mProc)(JNIEnv*);
97         const char* mName;
98     };
99 #endif
100 
101     static const RegJNIRec gRegJNI[] = {
102             REG_JNI(register_android_graphics_Canvas),
103             REG_JNI(register_android_graphics_Color),
104             // This needs to be before register_android_graphics_Graphics, or the latter
105             // will not be able to find the jmethodID for ColorSpace.get().
106             REG_JNI(register_android_graphics_ColorSpace),
107             REG_JNI(register_android_graphics_Graphics),
108             REG_JNI(register_android_graphics_Bitmap),
109             REG_JNI(register_android_graphics_BitmapFactory),
110             REG_JNI(register_android_graphics_BitmapRegionDecoder),
111             REG_JNI(register_android_graphics_RuntimeXfermode),
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_text_MeasuredText),
149             REG_JNI(register_android_graphics_text_LineBreaker),
150             REG_JNI(register_android_graphics_text_TextShaper),
151             REG_JNI(register_android_graphics_text_GraphemeBreak),
152             REG_JNI(register_android_graphics_MeshSpecification),
153             REG_JNI(register_android_graphics_Mesh),
154 
155             REG_JNI(register_android_util_PathParser),
156             REG_JNI(register_android_view_RenderNode),
157             REG_JNI(register_android_view_DisplayListCanvas),
158             REG_JNI(register_android_graphics_HardwareBufferRenderer),
159 
160             REG_JNI(register_android_view_ThreadedRenderer),
161     };
162 
163 } // namespace android
164 
init_android_graphics()165 void init_android_graphics() {
166     SkGraphics::Init();
167 }
168 
register_android_graphics_classes(JNIEnv * env)169 int register_android_graphics_classes(JNIEnv *env) {
170     JavaVM* vm = nullptr;
171     env->GetJavaVM(&vm);
172     GraphicsJNI::setJavaVM(vm);
173 
174     for (size_t i = 0; i < NELEM(android::gRegJNI); i++) {
175         if (android::gRegJNI[i].mProc(env) < 0) {
176 #ifndef NDEBUG
177             __android_log_print(ANDROID_LOG_FATAL, LOG_TAG, "JNI Error!!! %s failed to load\n",
178                                 android::gRegJNI[i].mName);
179 #endif
180             return -1;
181         }
182     }
183     return 0;
184 }
185 
186 using android::uirenderer::Properties;
187 using android::uirenderer::RenderPipelineType;
188 
zygote_preload_graphics()189 void zygote_preload_graphics() {
190     if (Properties::peekRenderPipelineType() == RenderPipelineType::SkiaGL) {
191         // Preload GL driver if HWUI renders with GL backend.
192         eglGetDisplay(EGL_DEFAULT_DISPLAY);
193     } else {
194         // Preload Vulkan driver if HWUI renders with Vulkan backend.
195         uint32_t apiVersion;
196         vkEnumerateInstanceVersion(&apiVersion);
197 
198         if (Properties::initializeGlAlways()) {
199             // Even though HWUI is rendering with Vulkan, some apps still use
200             // GL. Preload GL driver just in case. Since this happens prior to
201             // forking from the zygote, apps that do not use GL are unaffected.
202             // Any memory that (E)GL uses for this call is in shared memory,
203             // and this call only happens once.
204             eglGetDisplay(EGL_DEFAULT_DISPLAY);
205         }
206     }
207 }
208