From a7993241a96ff8832bac5fbc052d8a9ac6e72877 Mon Sep 17 00:00:00 2001 From: Andreas Gampe Date: Mon, 20 Feb 2017 16:18:24 -0800 Subject: [PATCH] Graphics: Make SurfaceTexture compile-time initializable Ensure that all static initializer code can be executed in the compiler. Move native field caching into the native method registration function. Bug: 34956610 Test: m Test: Device boots (Pixel) Change-Id: Icf68c8fbadcaaa133a152942a3a7ba7aba84a1df --- core/jni/android/graphics/SurfaceTexture.cpp | 6 +++++- graphics/java/android/graphics/SurfaceTexture.java | 7 ------- 2 files changed, 5 insertions(+), 8 deletions(-) diff --git a/core/jni/android/graphics/SurfaceTexture.cpp b/core/jni/android/graphics/SurfaceTexture.cpp index 00996730cb70d..2b1da25c632d0 100644 --- a/core/jni/android/graphics/SurfaceTexture.cpp +++ b/core/jni/android/graphics/SurfaceTexture.cpp @@ -34,6 +34,7 @@ #include "jni.h" #include "JNIHelp.h" +#include "ScopedLocalRef.h" // ---------------------------------------------------------------------------- @@ -385,7 +386,6 @@ static jboolean SurfaceTexture_isReleased(JNIEnv* env, jobject thiz) // ---------------------------------------------------------------------------- static const JNINativeMethod gSurfaceTextureMethods[] = { - {"nativeClassInit", "()V", (void*)SurfaceTexture_classInit }, {"nativeInit", "(ZIZLjava/lang/ref/WeakReference;)V", (void*)SurfaceTexture_init }, {"nativeFinalize", "()V", (void*)SurfaceTexture_finalize }, {"nativeSetDefaultBufferSize", "(II)V", (void*)SurfaceTexture_setDefaultBufferSize }, @@ -401,6 +401,10 @@ static const JNINativeMethod gSurfaceTextureMethods[] = { int register_android_graphics_SurfaceTexture(JNIEnv* env) { + // Cache some fields. + ScopedLocalRef klass(env, FindClassOrDie(env, kSurfaceTextureClassPathName)); + SurfaceTexture_classInit(env, klass.get()); + return RegisterMethodsOrDie(env, kSurfaceTextureClassPathName, gSurfaceTextureMethods, NELEM(gSurfaceTextureMethods)); } diff --git a/graphics/java/android/graphics/SurfaceTexture.java b/graphics/java/android/graphics/SurfaceTexture.java index efb46b90e399e..90bdd81a9d361 100644 --- a/graphics/java/android/graphics/SurfaceTexture.java +++ b/graphics/java/android/graphics/SurfaceTexture.java @@ -403,11 +403,4 @@ public class SurfaceTexture { private native int nativeGetQueuedCount(); private native void nativeRelease(); private native boolean nativeIsReleased(); - - /* - * We use a class initializer to allow the native code to cache some - * field offsets. - */ - private static native void nativeClassInit(); - static { nativeClassInit(); } }