Remove dependencies on headers outside UI module
This includes AndroidRuntime and core_jni_helper.h
Bug: 137655431
Test: CtsUiRenderingTestCases
Change-Id: If3d26f41eaf4981505ee47634097f3645fd563fd
diff --git a/libs/hwui/jni/AnimatedImageDrawable.cpp b/libs/hwui/jni/AnimatedImageDrawable.cpp
index 6c2a5a3..055075d 100644
--- a/libs/hwui/jni/AnimatedImageDrawable.cpp
+++ b/libs/hwui/jni/AnimatedImageDrawable.cpp
@@ -17,7 +17,6 @@
#include "GraphicsJNI.h"
#include "ImageDecoder.h"
#include "Utils.h"
-#include "core_jni_helpers.h"
#include <SkAndroidCodec.h>
#include <SkAnimatedImage.h>
diff --git a/libs/hwui/jni/Bitmap.cpp b/libs/hwui/jni/Bitmap.cpp
index 1f2ce8e..9981e89 100755
--- a/libs/hwui/jni/Bitmap.cpp
+++ b/libs/hwui/jni/Bitmap.cpp
@@ -26,9 +26,6 @@
#include <renderthread/RenderProxy.h>
#endif
-#include "core_jni_helpers.h"
-
-#include <jni.h>
#include <string.h>
#include <memory>
#include <string>
diff --git a/libs/hwui/jni/BitmapFactory.cpp b/libs/hwui/jni/BitmapFactory.cpp
index 48a5783..f3395b9 100644
--- a/libs/hwui/jni/BitmapFactory.cpp
+++ b/libs/hwui/jni/BitmapFactory.cpp
@@ -13,17 +13,15 @@
#include "SkStream.h"
#include "SkUtils.h"
#include "Utils.h"
-#include "core_jni_helpers.h"
#include <HardwareBitmapUploader.h>
#include <nativehelper/JNIHelp.h>
#include <androidfw/Asset.h>
#include <androidfw/ResourceTypes.h>
#include <cutils/compiler.h>
+#include <fcntl.h>
#include <memory>
-#include <netinet/in.h>
#include <stdio.h>
-#include <sys/mman.h>
#include <sys/stat.h>
jfieldID gOptions_justBoundsFieldID;
@@ -522,7 +520,9 @@
static jobject nativeDecodeFileDescriptor(JNIEnv* env, jobject clazz, jobject fileDescriptor,
jobject padding, jobject bitmapFactoryOptions, jlong inBitmapHandle, jlong colorSpaceHandle) {
-
+#ifndef __ANDROID__ // LayoutLib for Windows does not support F_DUPFD_CLOEXEC
+ return nullObjectReturn("Not supported on Windows");
+#else
NPE_CHECK_RETURN_ZERO(env, fileDescriptor);
int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
@@ -569,6 +569,7 @@
return doDecode(env, std::move(stream), padding, bitmapFactoryOptions, inBitmapHandle,
colorSpaceHandle);
+#endif
}
static jobject nativeDecodeAsset(JNIEnv* env, jobject clazz, jlong native_asset,
diff --git a/libs/hwui/jni/BitmapRegionDecoder.cpp b/libs/hwui/jni/BitmapRegionDecoder.cpp
index 3f80f03..7123513 100644
--- a/libs/hwui/jni/BitmapRegionDecoder.cpp
+++ b/libs/hwui/jni/BitmapRegionDecoder.cpp
@@ -28,12 +28,8 @@
#include "SkData.h"
#include "SkStream.h"
-#include "core_jni_helpers.h"
-
#include <HardwareBitmapUploader.h>
-#include <nativehelper/JNIHelp.h>
#include <androidfw/Asset.h>
-#include <jni.h>
#include <sys/stat.h>
#include <memory>
diff --git a/libs/hwui/jni/ByteBufferStreamAdaptor.cpp b/libs/hwui/jni/ByteBufferStreamAdaptor.cpp
index d443fd8..db5f6f6 100644
--- a/libs/hwui/jni/ByteBufferStreamAdaptor.cpp
+++ b/libs/hwui/jni/ByteBufferStreamAdaptor.cpp
@@ -1,7 +1,6 @@
#include "ByteBufferStreamAdaptor.h"
-#include "core_jni_helpers.h"
+#include "GraphicsJNI.h"
#include "Utils.h"
-#include <jni.h>
#include <SkStream.h>
diff --git a/libs/hwui/jni/Camera.cpp b/libs/hwui/jni/Camera.cpp
index da95497..a5e1adf 100644
--- a/libs/hwui/jni/Camera.cpp
+++ b/libs/hwui/jni/Camera.cpp
@@ -1,6 +1,3 @@
-#include "jni.h"
-#include "core_jni_helpers.h"
-
#include "SkCamera.h"
#include "GraphicsJNI.h"
diff --git a/libs/hwui/jni/CanvasProperty.cpp b/libs/hwui/jni/CanvasProperty.cpp
index c841d6a..684ee23 100644
--- a/libs/hwui/jni/CanvasProperty.cpp
+++ b/libs/hwui/jni/CanvasProperty.cpp
@@ -14,9 +14,7 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include <core_jni_helpers.h>
#include <hwui/Paint.h>
#include <utils/RefBase.h>
diff --git a/libs/hwui/jni/ColorFilter.cpp b/libs/hwui/jni/ColorFilter.cpp
index 164d35f..cef21f9 100644
--- a/libs/hwui/jni/ColorFilter.cpp
+++ b/libs/hwui/jni/ColorFilter.cpp
@@ -15,9 +15,7 @@
** limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include "SkColorFilter.h"
#include "SkColorMatrixFilter.h"
diff --git a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h
index fccd471..849418d 100644
--- a/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h
+++ b/libs/hwui/jni/CreateJavaOutputStreamAdaptor.h
@@ -1,7 +1,6 @@
#ifndef _ANDROID_GRAPHICS_CREATE_JAVA_OUTPUT_STREAM_ADAPTOR_H_
#define _ANDROID_GRAPHICS_CREATE_JAVA_OUTPUT_STREAM_ADAPTOR_H_
-//#include <android_runtime/AndroidRuntime.h>
#include "jni.h"
class SkMemoryStream;
diff --git a/libs/hwui/jni/FontFamily.cpp b/libs/hwui/jni/FontFamily.cpp
index f0cdb5e..a2fef1e 100644
--- a/libs/hwui/jni/FontFamily.cpp
+++ b/libs/hwui/jni/FontFamily.cpp
@@ -17,9 +17,6 @@
#undef LOG_TAG
#define LOG_TAG "Minikin"
-#include <nativehelper/JNIHelp.h>
-#include <core_jni_helpers.h>
-
#include "SkData.h"
#include "SkFontMgr.h"
#include "SkRefCnt.h"
@@ -27,7 +24,6 @@
#include "GraphicsJNI.h"
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
-#include <android_runtime/AndroidRuntime.h>
#include "Utils.h"
#include "FontUtils.h"
@@ -145,15 +141,11 @@
}
static void release_global_ref(const void* /*data*/, void* context) {
- JNIEnv* env = AndroidRuntime::getJNIEnv();
+ JNIEnv* env = GraphicsJNI::getJNIEnv();
bool needToAttach = (env == NULL);
if (needToAttach) {
- JavaVMAttachArgs args;
- args.version = JNI_VERSION_1_4;
- args.name = "release_font_data";
- args.group = NULL;
- jint result = AndroidRuntime::getJavaVM()->AttachCurrentThread(&env, &args);
- if (result != JNI_OK) {
+ env = GraphicsJNI::attachJNIEnv("release_font_data");
+ if (env == nullptr) {
ALOGE("failed to attach to thread to release global ref.");
return;
}
@@ -163,7 +155,7 @@
env->DeleteGlobalRef(obj);
if (needToAttach) {
- AndroidRuntime::getJavaVM()->DetachCurrentThread();
+ GraphicsJNI::detachJNIEnv();
}
}
diff --git a/libs/hwui/jni/FontUtils.cpp b/libs/hwui/jni/FontUtils.cpp
index 0cf61b9..654c5fd 100644
--- a/libs/hwui/jni/FontUtils.cpp
+++ b/libs/hwui/jni/FontUtils.cpp
@@ -16,8 +16,7 @@
#include "FontUtils.h"
-#include <nativehelper/JNIHelp.h>
-#include <core_jni_helpers.h>
+#include "graphics_jni_helpers.h"
namespace android {
namespace {
diff --git a/libs/hwui/jni/Graphics.cpp b/libs/hwui/jni/Graphics.cpp
index 3795864..f76ecb4 100644
--- a/libs/hwui/jni/Graphics.cpp
+++ b/libs/hwui/jni/Graphics.cpp
@@ -1,23 +1,55 @@
#undef LOG_TAG
#define LOG_TAG "GraphicsJNI"
+#include <assert.h>
#include <unistd.h>
-#include <sys/mman.h>
#include "jni.h"
#include <nativehelper/JNIHelp.h>
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include "SkCanvas.h"
#include "SkMath.h"
#include "SkRegion.h"
-#include <android_runtime/AndroidRuntime.h>
#include <cutils/ashmem.h>
#include <hwui/Canvas.h>
using namespace android;
+/*static*/ JavaVM* GraphicsJNI::mJavaVM = nullptr;
+
+void GraphicsJNI::setJavaVM(JavaVM* javaVM) {
+ mJavaVM = javaVM;
+}
+
+/** return a pointer to the JNIEnv for this thread */
+JNIEnv* GraphicsJNI::getJNIEnv() {
+ assert(mJavaVM != nullptr);
+ JNIEnv* env;
+ if (mJavaVM->GetEnv((void**) &env, JNI_VERSION_1_4) != JNI_OK) {
+ return nullptr;
+ }
+ return env;
+}
+
+/** create a JNIEnv* for this thread or assert if one already exists */
+JNIEnv* GraphicsJNI::attachJNIEnv(const char* envName) {
+ assert(getJNIEnv() == nullptr);
+ JNIEnv* env = nullptr;
+ JavaVMAttachArgs args = { JNI_VERSION_1_4, envName, NULL };
+ int result = mJavaVM->AttachCurrentThread(&env, (void*) &args);
+ if (result != JNI_OK) {
+ ALOGE("thread attach failed: %#x", result);
+ }
+ return env;
+}
+
+/** detach the current thread from the JavaVM */
+void GraphicsJNI::detachJNIEnv() {
+ assert(mJavaVM != nullptr);
+ mJavaVM->DetachCurrentThread();
+}
+
void doThrowNPE(JNIEnv* env) {
jniThrowNullPointerException(env, NULL);
}
diff --git a/libs/hwui/jni/GraphicsJNI.h b/libs/hwui/jni/GraphicsJNI.h
index 1e49765..4bf3ed1 100644
--- a/libs/hwui/jni/GraphicsJNI.h
+++ b/libs/hwui/jni/GraphicsJNI.h
@@ -10,10 +10,11 @@
#include "SkPoint.h"
#include "SkRect.h"
#include "SkColorSpace.h"
-#include <jni.h>
#include <hwui/Canvas.h>
#include <hwui/Bitmap.h>
+#include "graphics_jni_helpers.h"
+
class SkBitmapRegionDecoder;
class SkCanvas;
@@ -39,6 +40,20 @@
kLastEnum_LegacyBitmapConfig = kHardware_LegacyBitmapConfig
};
+ static void setJavaVM(JavaVM* javaVM);
+
+ /** returns a pointer to the JavaVM provided when we initialized the module */
+ static JavaVM* getJavaVM() { return mJavaVM; }
+
+ /** return a pointer to the JNIEnv for this thread */
+ static JNIEnv* getJNIEnv();
+
+ /** create a JNIEnv* for this thread or assert if one already exists */
+ static JNIEnv* attachJNIEnv(const char* envName);
+
+ /** detach the current thread from the JavaVM */
+ static void detachJNIEnv();
+
// returns true if an exception is set (and dumps it out to the Log)
static bool hasException(JNIEnv*);
@@ -131,6 +146,10 @@
* above.
*/
static SkColor4f convertColorLong(jlong color);
+
+private:
+ /* JNI JavaVM pointer */
+ static JavaVM* mJavaVM;
};
class HeapAllocator : public SkBRDAllocator {
diff --git a/libs/hwui/jni/GraphicsStatsService.cpp b/libs/hwui/jni/GraphicsStatsService.cpp
index 2ce392d..e925946 100644
--- a/libs/hwui/jni/GraphicsStatsService.cpp
+++ b/libs/hwui/jni/GraphicsStatsService.cpp
@@ -18,16 +18,14 @@
#define LOG_TAG "GraphicsStatsService"
#include <JankTracker.h>
-#include <jni.h>
#include <log/log.h>
-#include <nativehelper/JNIHelp.h>
#include <nativehelper/ScopedPrimitiveArray.h>
#include <nativehelper/ScopedUtfChars.h>
#include <service/GraphicsStatsService.h>
#include <stats_event.h>
#include <stats_pull_atom_callback.h>
#include <statslog.h>
-#include "core_jni_helpers.h"
+#include "GraphicsJNI.h"
namespace android {
@@ -116,7 +114,7 @@
static jmethodID gGraphicsStatsService_pullGraphicsStatsMethodID;
static JNIEnv* getJNIEnv() {
- JavaVM* vm = AndroidRuntime::getJavaVM();
+ JavaVM* vm = GraphicsJNI::getJavaVM();
JNIEnv* env = nullptr;
if (vm->GetEnv(reinterpret_cast<void**>(&env), JNI_VERSION_1_6) != JNI_OK) {
if (vm->AttachCurrentThreadAsDaemon(&env, nullptr) != JNI_OK) {
diff --git a/libs/hwui/jni/ImageDecoder.cpp b/libs/hwui/jni/ImageDecoder.cpp
index e17e057..b6b3785 100644
--- a/libs/hwui/jni/ImageDecoder.cpp
+++ b/libs/hwui/jni/ImageDecoder.cpp
@@ -22,7 +22,6 @@
#include "ImageDecoder.h"
#include "NinePatchPeeker.h"
#include "Utils.h"
-#include "core_jni_helpers.h"
#include <hwui/Bitmap.h>
#include <hwui/ImageDecoder.h>
@@ -34,7 +33,7 @@
#include <SkStream.h>
#include <androidfw/Asset.h>
-#include <jni.h>
+#include <fcntl.h>
#include <sys/stat.h>
using namespace android;
@@ -154,6 +153,9 @@
static jobject ImageDecoder_nCreateFd(JNIEnv* env, jobject /*clazz*/,
jobject fileDescriptor, jboolean preferAnimation, jobject source) {
+#ifndef __ANDROID__ // LayoutLib for Windows does not support F_DUPFD_CLOEXEC
+ return throw_exception(env, kSourceException, "Only supported on Android", nullptr, source);
+#else
int descriptor = jniGetFDFromFileDescriptor(env, fileDescriptor);
struct stat fdStat;
@@ -172,6 +174,7 @@
std::unique_ptr<SkFILEStream> fileStream(new SkFILEStream(file));
return native_create(env, std::move(fileStream), source, preferAnimation);
+#endif
}
static jobject ImageDecoder_nCreateInputStream(JNIEnv* env, jobject /*clazz*/,
diff --git a/libs/hwui/jni/Interpolator.cpp b/libs/hwui/jni/Interpolator.cpp
index fa28359..146d634 100644
--- a/libs/hwui/jni/Interpolator.cpp
+++ b/libs/hwui/jni/Interpolator.cpp
@@ -1,8 +1,5 @@
#include "GraphicsJNI.h"
#include "SkInterpolator.h"
-#include "core_jni_helpers.h"
-
-#include <jni.h>
static jlong Interpolator_constructor(JNIEnv* env, jobject clazz, jint valueCount, jint frameCount)
{
diff --git a/libs/hwui/jni/MaskFilter.cpp b/libs/hwui/jni/MaskFilter.cpp
index 33d346f..5383032 100644
--- a/libs/hwui/jni/MaskFilter.cpp
+++ b/libs/hwui/jni/MaskFilter.cpp
@@ -4,10 +4,6 @@
#include "SkBlurMaskFilter.h"
#include "SkTableMaskFilter.h"
-#include "core_jni_helpers.h"
-
-#include <jni.h>
-
static void ThrowIAE_IfNull(JNIEnv* env, void* ptr) {
if (NULL == ptr) {
doThrowIAE(env);
diff --git a/libs/hwui/jni/Movie.cpp b/libs/hwui/jni/Movie.cpp
index 4c10a85..ede0ca8 100644
--- a/libs/hwui/jni/Movie.cpp
+++ b/libs/hwui/jni/Movie.cpp
@@ -6,13 +6,11 @@
#include "SkStream.h"
#include "SkUtils.h"
#include "Utils.h"
-#include "core_jni_helpers.h"
#include <androidfw/Asset.h>
#include <androidfw/ResourceTypes.h>
#include <hwui/Canvas.h>
#include <hwui/Paint.h>
-#include <jni.h>
#include <netinet/in.h>
static jclass gMovie_class;
diff --git a/libs/hwui/jni/NinePatch.cpp b/libs/hwui/jni/NinePatch.cpp
index 9886155..6942017 100644
--- a/libs/hwui/jni/NinePatch.cpp
+++ b/libs/hwui/jni/NinePatch.cpp
@@ -31,9 +31,6 @@
#include "NinePatchPeeker.h"
#include "NinePatchUtils.h"
-#include <nativehelper/JNIHelp.h>
-#include "core_jni_helpers.h"
-
jclass gInsetStruct_class;
jmethodID gInsetStruct_constructorMethodID;
diff --git a/libs/hwui/jni/Paint.cpp b/libs/hwui/jni/Paint.cpp
index 7dda47b..df8635a 100644
--- a/libs/hwui/jni/Paint.cpp
+++ b/libs/hwui/jni/Paint.cpp
@@ -20,9 +20,7 @@
#include <utils/Log.h>
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include <nativehelper/ScopedStringChars.h>
#include <nativehelper/ScopedUtfChars.h>
#include <nativehelper/ScopedPrimitiveArray.h>
diff --git a/libs/hwui/jni/PaintFilter.cpp b/libs/hwui/jni/PaintFilter.cpp
index 4fe9140..ec115b4 100644
--- a/libs/hwui/jni/PaintFilter.cpp
+++ b/libs/hwui/jni/PaintFilter.cpp
@@ -15,11 +15,7 @@
** limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include <android_runtime/AndroidRuntime.h>
-
-#include "core_jni_helpers.h"
#include "hwui/Paint.h"
#include "hwui/PaintFilter.h"
diff --git a/libs/hwui/jni/Path.cpp b/libs/hwui/jni/Path.cpp
index 4814452..d67bcf2 100644
--- a/libs/hwui/jni/Path.cpp
+++ b/libs/hwui/jni/Path.cpp
@@ -20,9 +20,7 @@
// To change this file, either edit the include, or device/tools/gluemaker/main.cpp,
// or one of the auxilary file specifications in device/tools/gluemaker.
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include "SkPath.h"
#include "SkPathOps.h"
diff --git a/libs/hwui/jni/PathEffect.cpp b/libs/hwui/jni/PathEffect.cpp
index a4992de..f99bef7 100644
--- a/libs/hwui/jni/PathEffect.cpp
+++ b/libs/hwui/jni/PathEffect.cpp
@@ -4,9 +4,6 @@
#include "SkDashPathEffect.h"
#include "SkDiscretePathEffect.h"
#include "SkPathEffect.h"
-#include "core_jni_helpers.h"
-
-#include <jni.h>
class SkPathEffectGlue {
public:
diff --git a/libs/hwui/jni/PathMeasure.cpp b/libs/hwui/jni/PathMeasure.cpp
index 70e528d..acf893e 100644
--- a/libs/hwui/jni/PathMeasure.cpp
+++ b/libs/hwui/jni/PathMeasure.cpp
@@ -15,9 +15,7 @@
** limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include <core_jni_helpers.h>
#include "SkPathMeasure.h"
diff --git a/libs/hwui/jni/Region.cpp b/libs/hwui/jni/Region.cpp
index 87662f7..c95bcea 100644
--- a/libs/hwui/jni/Region.cpp
+++ b/libs/hwui/jni/Region.cpp
@@ -24,9 +24,6 @@
#include "android_os_Parcel.h"
#include "android_util_Binder.h"
-#include <jni.h>
-#include <core_jni_helpers.h>
-
namespace android {
static jfieldID gRegion_nativeInstanceFieldID;
diff --git a/libs/hwui/jni/Shader.cpp b/libs/hwui/jni/Shader.cpp
index f5e2a52..0f68376 100644
--- a/libs/hwui/jni/Shader.cpp
+++ b/libs/hwui/jni/Shader.cpp
@@ -4,11 +4,8 @@
#include "SkImagePriv.h"
#include "SkShader.h"
#include "SkBlendMode.h"
-#include "core_jni_helpers.h"
#include "include/effects/SkRuntimeEffect.h"
-#include <jni.h>
-
#include <vector>
using namespace android::uirenderer;
diff --git a/libs/hwui/jni/Typeface.cpp b/libs/hwui/jni/Typeface.cpp
index 4ce56ba..2a5f402 100644
--- a/libs/hwui/jni/Typeface.cpp
+++ b/libs/hwui/jni/Typeface.cpp
@@ -14,9 +14,6 @@
* limitations under the License.
*/
-#include "jni.h"
-#include "core_jni_helpers.h"
-
#include "FontUtils.h"
#include "GraphicsJNI.h"
#include <nativehelper/ScopedPrimitiveArray.h>
diff --git a/libs/hwui/jni/YuvToJpegEncoder.cpp b/libs/hwui/jni/YuvToJpegEncoder.cpp
index 09adc82..689cf0b 100644
--- a/libs/hwui/jni/YuvToJpegEncoder.cpp
+++ b/libs/hwui/jni/YuvToJpegEncoder.cpp
@@ -4,9 +4,7 @@
#include <ui/PixelFormat.h>
#include <hardware/hardware.h>
-#include "core_jni_helpers.h"
-
-#include <jni.h>
+#include "graphics_jni_helpers.h"
YuvToJpegEncoder* YuvToJpegEncoder::create(int format, int* strides) {
// Only ImageFormat.NV21 and ImageFormat.YUY2 are supported
diff --git a/libs/hwui/jni/android_graphics_Canvas.cpp b/libs/hwui/jni/android_graphics_Canvas.cpp
index 0ad3339..4aff3e5 100644
--- a/libs/hwui/jni/android_graphics_Canvas.cpp
+++ b/libs/hwui/jni/android_graphics_Canvas.cpp
@@ -14,9 +14,7 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#ifdef __ANDROID_
#include <android/api-level.h>
diff --git a/libs/hwui/jni/android_graphics_ColorSpace.cpp b/libs/hwui/jni/android_graphics_ColorSpace.cpp
index 7648fd0..232fd71 100644
--- a/libs/hwui/jni/android_graphics_ColorSpace.cpp
+++ b/libs/hwui/jni/android_graphics_ColorSpace.cpp
@@ -14,12 +14,11 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include "SkColor.h"
#include "SkColorSpace.h"
+#include "SkHalf.h"
using namespace android;
@@ -42,9 +41,13 @@
///////////////////////////////////////////////////////////////////////////////
static float halfToFloat(uint16_t bits) {
- __fp16 h;
- memcpy(&h, &bits, 2);
- return (float)h;
+#ifdef __ANDROID__ // __fp16 is not defined on non-Android builds
+ __fp16 h;
+ memcpy(&h, &bits, 2);
+ return (float)h;
+#else
+ return SkHalfToFloat(bits);
+#endif
}
SkColor4f GraphicsJNI::convertColorLong(jlong color) {
diff --git a/libs/hwui/jni/android_graphics_DisplayListCanvas.cpp b/libs/hwui/jni/android_graphics_DisplayListCanvas.cpp
index ca10c1e..54822f1 100644
--- a/libs/hwui/jni/android_graphics_DisplayListCanvas.cpp
+++ b/libs/hwui/jni/android_graphics_DisplayListCanvas.cpp
@@ -14,11 +14,8 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include <nativehelper/JNIHelp.h>
-#include <android_runtime/AndroidRuntime.h>
#ifdef __ANDROID__ // Layoutlib does not support Looper and device properties
#include <utils/Looper.h>
#endif
@@ -36,8 +33,6 @@
#include <renderthread/RenderProxy.h>
#endif
-#include "core_jni_helpers.h"
-
namespace android {
using namespace uirenderer;
diff --git a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
index 76bfce8..49c7fcd 100644
--- a/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
+++ b/libs/hwui/jni/android_graphics_HardwareRenderer.cpp
@@ -44,8 +44,6 @@
#include <atomic>
#include "android_graphics_HardwareRendererObserver.h"
-#include "core_jni_helpers.h"
-#include "jni.h"
namespace android {
diff --git a/libs/hwui/jni/android_graphics_HardwareRendererObserver.cpp b/libs/hwui/jni/android_graphics_HardwareRendererObserver.cpp
index 89b77b0..5b3e656 100644
--- a/libs/hwui/jni/android_graphics_HardwareRendererObserver.cpp
+++ b/libs/hwui/jni/android_graphics_HardwareRendererObserver.cpp
@@ -16,7 +16,7 @@
#include "android_graphics_HardwareRendererObserver.h"
-#include "core_jni_helpers.h"
+#include "graphics_jni_helpers.h"
#include "nativehelper/jni_macros.h"
#include <array>
diff --git a/libs/hwui/jni/android_graphics_Matrix.cpp b/libs/hwui/jni/android_graphics_Matrix.cpp
index 1336976..7338ef2 100644
--- a/libs/hwui/jni/android_graphics_Matrix.cpp
+++ b/libs/hwui/jni/android_graphics_Matrix.cpp
@@ -18,9 +18,6 @@
#include "GraphicsJNI.h"
#include "Matrix.h"
#include "SkMatrix.h"
-#include "core_jni_helpers.h"
-
-#include <jni.h>
namespace android {
diff --git a/libs/hwui/jni/android_graphics_Picture.cpp b/libs/hwui/jni/android_graphics_Picture.cpp
index 1d085e5..403efb2 100644
--- a/libs/hwui/jni/android_graphics_Picture.cpp
+++ b/libs/hwui/jni/android_graphics_Picture.cpp
@@ -19,12 +19,9 @@
#include "Picture.h"
#include "SkCanvas.h"
#include "SkStream.h"
-#include "core_jni_helpers.h"
-#include "nativehelper/jni_macros.h"
-
-#include <jni.h>
#include <array>
+#include "nativehelper/jni_macros.h"
namespace android {
diff --git a/libs/hwui/jni/android_graphics_RenderNode.cpp b/libs/hwui/jni/android_graphics_RenderNode.cpp
index a5b9e5e..85c802b 100644
--- a/libs/hwui/jni/android_graphics_RenderNode.cpp
+++ b/libs/hwui/jni/android_graphics_RenderNode.cpp
@@ -15,10 +15,7 @@
*/
#define ATRACE_TAG ATRACE_TAG_VIEW
-#include "jni.h"
#include "GraphicsJNI.h"
-#include <nativehelper/JNIHelp.h>
-#include <android_runtime/AndroidRuntime.h>
#include <Animator.h>
#include <DamageAccumulator.h>
@@ -31,8 +28,6 @@
#include <hwui/Paint.h>
#include <utils/TraceUtils.h>
-#include "core_jni_helpers.h"
-
namespace android {
using namespace uirenderer;
diff --git a/libs/hwui/jni/android_graphics_TextureLayer.cpp b/libs/hwui/jni/android_graphics_TextureLayer.cpp
index e8043d2..bd20269 100644
--- a/libs/hwui/jni/android_graphics_TextureLayer.cpp
+++ b/libs/hwui/jni/android_graphics_TextureLayer.cpp
@@ -14,11 +14,8 @@
* limitations under the License.
*/
-#include "jni.h"
-#include <nativehelper/JNIHelp.h>
-
#include <android/surface_texture_jni.h>
-#include "core_jni_helpers.h"
+#include "graphics_jni_helpers.h"
#include <hwui/Paint.h>
#include <SkMatrix.h>
diff --git a/libs/hwui/jni/android_graphics_animation_NativeInterpolatorFactory.cpp b/libs/hwui/jni/android_graphics_animation_NativeInterpolatorFactory.cpp
index 2073ac2..764eff9 100644
--- a/libs/hwui/jni/android_graphics_animation_NativeInterpolatorFactory.cpp
+++ b/libs/hwui/jni/android_graphics_animation_NativeInterpolatorFactory.cpp
@@ -16,12 +16,10 @@
#define LOG_TAG "OpenGLRenderer"
-#include "jni.h"
-#include <nativehelper/JNIHelp.h>
-#include <cutils/log.h>
-#include "core_jni_helpers.h"
-
#include <Interpolator.h>
+#include <cutils/log.h>
+
+#include "graphics_jni_helpers.h"
namespace android {
diff --git a/libs/hwui/jni/android_graphics_animation_RenderNodeAnimator.cpp b/libs/hwui/jni/android_graphics_animation_RenderNodeAnimator.cpp
index 878d4fc..c6d26f8 100644
--- a/libs/hwui/jni/android_graphics_animation_RenderNodeAnimator.cpp
+++ b/libs/hwui/jni/android_graphics_animation_RenderNodeAnimator.cpp
@@ -16,15 +16,11 @@
#define LOG_TAG "OpenGLRenderer"
-#include "jni.h"
-#include <nativehelper/JNIHelp.h>
-#include <android_runtime/AndroidRuntime.h>
-
#include <Animator.h>
#include <Interpolator.h>
#include <RenderProperties.h>
-#include "core_jni_helpers.h"
+#include "graphics_jni_helpers.h"
namespace android {
diff --git a/libs/hwui/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp b/libs/hwui/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
index 1246556..b3121e7 100644
--- a/libs/hwui/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
+++ b/libs/hwui/jni/android_graphics_drawable_AnimatedVectorDrawable.cpp
@@ -16,9 +16,7 @@
#include "android/log.h"
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include "Animator.h"
#include "Interpolator.h"
diff --git a/libs/hwui/jni/android_graphics_drawable_VectorDrawable.cpp b/libs/hwui/jni/android_graphics_drawable_VectorDrawable.cpp
index 58a2379..8a26296 100644
--- a/libs/hwui/jni/android_graphics_drawable_VectorDrawable.cpp
+++ b/libs/hwui/jni/android_graphics_drawable_VectorDrawable.cpp
@@ -15,8 +15,6 @@
*/
#include "GraphicsJNI.h"
-#include "jni.h"
-#include "core_jni_helpers.h"
#include "PathParser.h"
#include "VectorDrawable.h"
diff --git a/libs/hwui/jni/android_nio_utils.cpp b/libs/hwui/jni/android_nio_utils.cpp
index 1e6d49e..c2b09c1 100644
--- a/libs/hwui/jni/android_nio_utils.cpp
+++ b/libs/hwui/jni/android_nio_utils.cpp
@@ -16,7 +16,7 @@
#include "android_nio_utils.h"
-#include "core_jni_helpers.h"
+#include <nativehelper/JNIHelp.h>
namespace android {
diff --git a/libs/hwui/jni/android_util_PathParser.cpp b/libs/hwui/jni/android_util_PathParser.cpp
index 10efb95..df5e9cd 100644
--- a/libs/hwui/jni/android_util_PathParser.cpp
+++ b/libs/hwui/jni/android_util_PathParser.cpp
@@ -14,7 +14,6 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
#include <PathParser.h>
@@ -22,7 +21,6 @@
#include <utils/VectorDrawableUtils.h>
#include <android/log.h>
-#include "core_jni_helpers.h"
namespace android {
diff --git a/libs/hwui/jni/fonts/Font.cpp b/libs/hwui/jni/fonts/Font.cpp
index d538832..5714cd1 100644
--- a/libs/hwui/jni/fonts/Font.cpp
+++ b/libs/hwui/jni/fonts/Font.cpp
@@ -17,16 +17,12 @@
#undef LOG_TAG
#define LOG_TAG "Minikin"
-#include <nativehelper/JNIHelp.h>
-#include <core_jni_helpers.h>
-
#include "SkData.h"
#include "SkFontMgr.h"
#include "SkRefCnt.h"
#include "SkTypeface.h"
#include "GraphicsJNI.h"
#include <nativehelper/ScopedUtfChars.h>
-#include <android_runtime/AndroidRuntime.h>
#include "Utils.h"
#include "FontUtils.h"
@@ -52,14 +48,11 @@
}
static void release_global_ref(const void* /*data*/, void* context) {
- JNIEnv* env = AndroidRuntime::getJNIEnv();
- if (env == nullptr) {
- JavaVMAttachArgs args;
- args.version = JNI_VERSION_1_4;
- args.name = "release_font_data";
- args.group = nullptr;
- jint result = AndroidRuntime::getJavaVM()->AttachCurrentThread(&env, &args);
- if (result != JNI_OK) {
+ JNIEnv* env = GraphicsJNI::getJNIEnv();
+ bool needToAttach = (env == nullptr);
+ if (needToAttach) {
+ env = GraphicsJNI::attachJNIEnv("release_font_data");
+ if (env == nullptr) {
ALOGE("failed to attach to thread to release global ref.");
return;
}
diff --git a/libs/hwui/jni/fonts/FontFamily.cpp b/libs/hwui/jni/fonts/FontFamily.cpp
index 26d70a0..df619d9 100644
--- a/libs/hwui/jni/fonts/FontFamily.cpp
+++ b/libs/hwui/jni/fonts/FontFamily.cpp
@@ -17,9 +17,8 @@
#undef LOG_TAG
#define LOG_TAG "Minikin"
-#include <nativehelper/JNIHelp.h>
+#include "graphics_jni_helpers.h"
#include <nativehelper/ScopedUtfChars.h>
-#include <core_jni_helpers.h>
#include "FontUtils.h"
diff --git a/libs/hwui/jni/graphics_jni_helpers.h b/libs/hwui/jni/graphics_jni_helpers.h
new file mode 100644
index 0000000..b97cc6a
--- /dev/null
+++ b/libs/hwui/jni/graphics_jni_helpers.h
@@ -0,0 +1,106 @@
+/*
+ * Copyright (C) 2014 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef GRAPHICS_JNI_HELPERS
+#define GRAPHICS_JNI_HELPERS
+
+#include <log/log.h>
+#include <nativehelper/JNIHelp.h>
+#include <nativehelper/scoped_local_ref.h>
+#include <nativehelper/scoped_utf_chars.h>
+#include <string>
+
+// Host targets (layoutlib) do not differentiate between regular and critical native methods,
+// and they need all the JNI methods to have JNIEnv* and jclass/jobject as their first two arguments.
+// The following macro allows to have those arguments when compiling for host while omitting them when
+// compiling for Android.
+#ifdef __ANDROID__
+#define CRITICAL_JNI_PARAMS
+#define CRITICAL_JNI_PARAMS_COMMA
+#else
+#define CRITICAL_JNI_PARAMS JNIEnv*, jclass
+#define CRITICAL_JNI_PARAMS_COMMA JNIEnv*, jclass,
+#endif
+
+namespace android {
+
+// Defines some helpful functions.
+
+static inline jclass FindClassOrDie(JNIEnv* env, const char* class_name) {
+ jclass clazz = env->FindClass(class_name);
+ LOG_ALWAYS_FATAL_IF(clazz == NULL, "Unable to find class %s", class_name);
+ return clazz;
+}
+
+static inline jfieldID GetFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
+ const char* field_signature) {
+ jfieldID res = env->GetFieldID(clazz, field_name, field_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
+ return res;
+}
+
+static inline jmethodID GetMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
+ const char* method_signature) {
+ jmethodID res = env->GetMethodID(clazz, method_name, method_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find method %s", method_name);
+ return res;
+}
+
+static inline jfieldID GetStaticFieldIDOrDie(JNIEnv* env, jclass clazz, const char* field_name,
+ const char* field_signature) {
+ jfieldID res = env->GetStaticFieldID(clazz, field_name, field_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static field %s", field_name);
+ return res;
+}
+
+static inline jmethodID GetStaticMethodIDOrDie(JNIEnv* env, jclass clazz, const char* method_name,
+ const char* method_signature) {
+ jmethodID res = env->GetStaticMethodID(clazz, method_name, method_signature);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to find static method %s", method_name);
+ return res;
+}
+
+template <typename T>
+static inline T MakeGlobalRefOrDie(JNIEnv* env, T in) {
+ jobject res = env->NewGlobalRef(in);
+ LOG_ALWAYS_FATAL_IF(res == NULL, "Unable to create global reference.");
+ return static_cast<T>(res);
+}
+
+static inline int RegisterMethodsOrDie(JNIEnv* env, const char* className,
+ const JNINativeMethod* gMethods, int numMethods) {
+ int res = jniRegisterNativeMethods(env, className, gMethods, numMethods);
+ LOG_ALWAYS_FATAL_IF(res < 0, "Unable to register native methods.");
+ return res;
+}
+
+/**
+ * Read the specified field from jobject, and convert to std::string.
+ * If the field cannot be obtained, return defaultValue.
+ */
+static inline std::string getStringField(JNIEnv* env, jobject obj, jfieldID fieldId,
+ const char* defaultValue) {
+ ScopedLocalRef<jstring> strObj(env, jstring(env->GetObjectField(obj, fieldId)));
+ if (strObj != nullptr) {
+ ScopedUtfChars chars(env, strObj.get());
+ return std::string(chars.c_str());
+ }
+ return std::string(defaultValue);
+}
+
+} // namespace android
+
+#endif // GRAPHICS_JNI_HELPERS
diff --git a/libs/hwui/jni/pdf/PdfDocument.cpp b/libs/hwui/jni/pdf/PdfDocument.cpp
index 5f67d30..d21eb3f 100644
--- a/libs/hwui/jni/pdf/PdfDocument.cpp
+++ b/libs/hwui/jni/pdf/PdfDocument.cpp
@@ -14,9 +14,7 @@
* limitations under the License.
*/
-#include "jni.h"
#include "GraphicsJNI.h"
-#include "core_jni_helpers.h"
#include <vector>
#include "CreateJavaOutputStreamAdaptor.h"
diff --git a/libs/hwui/jni/pdf/PdfEditor.cpp b/libs/hwui/jni/pdf/PdfEditor.cpp
index 545f4c5..828d6e3 100644
--- a/libs/hwui/jni/pdf/PdfEditor.cpp
+++ b/libs/hwui/jni/pdf/PdfEditor.cpp
@@ -27,8 +27,7 @@
#include "PdfUtils.h"
-#include "jni.h"
-#include <nativehelper/JNIHelp.h>
+#include "graphics_jni_helpers.h"
#pragma GCC diagnostic push
#pragma GCC diagnostic ignored "-Wdelete-non-virtual-dtor"
@@ -40,8 +39,6 @@
#include "SkMatrix.h"
-#include <core_jni_helpers.h>
-
namespace android {
enum PageBox {PAGE_BOX_MEDIA, PAGE_BOX_CROP};
diff --git a/libs/hwui/jni/pdf/PdfRenderer.cpp b/libs/hwui/jni/pdf/PdfRenderer.cpp
index 761830b..cc1f961 100644
--- a/libs/hwui/jni/pdf/PdfRenderer.cpp
+++ b/libs/hwui/jni/pdf/PdfRenderer.cpp
@@ -16,14 +16,11 @@
#include "PdfUtils.h"
-#include "jni.h"
-#include <nativehelper/JNIHelp.h>
#include "GraphicsJNI.h"
#include "SkBitmap.h"
#include "SkMatrix.h"
#include "fpdfview.h"
-#include "core_jni_helpers.h"
#include <vector>
#include <utils/Log.h>
#include <unistd.h>
diff --git a/libs/hwui/jni/scoped_nullable_primitive_array.h b/libs/hwui/jni/scoped_nullable_primitive_array.h
new file mode 100644
index 0000000..77f4c9d
--- /dev/null
+++ b/libs/hwui/jni/scoped_nullable_primitive_array.h
@@ -0,0 +1,103 @@
+/*
+ * Copyright (C) 2017 The Android Open Source Project
+ *
+ * Licensed under the Apache License, Version 2.0 (the "License");
+ * you may not use this file except in compliance with the License.
+ * You may obtain a copy of the License at
+ *
+ * http://www.apache.org/licenses/LICENSE-2.0
+ *
+ * Unless required by applicable law or agreed to in writing, software
+ * distributed under the License is distributed on an "AS IS" BASIS,
+ * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
+ * See the License for the specific language governing permissions and
+ * limitations under the License.
+ */
+
+#ifndef SCOPED_NULLABLE_PRIMITIVE_ARRAY_H
+#define SCOPED_NULLABLE_PRIMITIVE_ARRAY_H
+
+#include <jni.h>
+
+namespace android {
+
+#define ARRAY_TRAITS(ARRAY_TYPE, POINTER_TYPE, NAME) \
+class NAME ## ArrayTraits { \
+public: \
+ static constexpr void getArrayRegion(JNIEnv* env, ARRAY_TYPE array, size_t start, \
+ size_t len, POINTER_TYPE out) { \
+ env->Get ## NAME ## ArrayRegion(array, start, len, out); \
+ } \
+ \
+ static constexpr POINTER_TYPE getArrayElements(JNIEnv* env, ARRAY_TYPE array) { \
+ return env->Get ## NAME ## ArrayElements(array, nullptr); \
+ } \
+ \
+ static constexpr void releaseArrayElements(JNIEnv* env, ARRAY_TYPE array, \
+ POINTER_TYPE buffer, jint mode) { \
+ env->Release ## NAME ## ArrayElements(array, buffer, mode); \
+ } \
+}; \
+
+ARRAY_TRAITS(jbooleanArray, jboolean*, Boolean)
+ARRAY_TRAITS(jbyteArray, jbyte*, Byte)
+ARRAY_TRAITS(jcharArray, jchar*, Char)
+ARRAY_TRAITS(jdoubleArray, jdouble*, Double)
+ARRAY_TRAITS(jfloatArray, jfloat*, Float)
+ARRAY_TRAITS(jintArray, jint*, Int)
+ARRAY_TRAITS(jlongArray, jlong*, Long)
+ARRAY_TRAITS(jshortArray, jshort*, Short)
+
+#undef ARRAY_TRAITS
+
+template<typename JavaArrayType, typename PrimitiveType, class Traits, size_t preallocSize = 10>
+class ScopedArrayRO {
+public:
+ ScopedArrayRO(JNIEnv* env, JavaArrayType javaArray) : mEnv(env), mJavaArray(javaArray) {
+ if (mJavaArray == nullptr) {
+ mSize = 0;
+ mRawArray = nullptr;
+ } else {
+ mSize = mEnv->GetArrayLength(mJavaArray);
+ if (mSize <= preallocSize) {
+ Traits::getArrayRegion(mEnv, mJavaArray, 0, mSize, mBuffer);
+ mRawArray = mBuffer;
+ } else {
+ mRawArray = Traits::getArrayElements(mEnv, mJavaArray);
+ }
+ }
+ }
+
+ ~ScopedArrayRO() {
+ if (mRawArray != nullptr && mRawArray != mBuffer) {
+ Traits::releaseArrayElements(mEnv, mJavaArray, mRawArray, JNI_ABORT);
+ }
+ }
+
+ const PrimitiveType* get() const { return mRawArray; }
+ const PrimitiveType& operator[](size_t n) const { return mRawArray[n]; }
+ size_t size() const { return mSize; }
+
+private:
+ JNIEnv* const mEnv;
+ JavaArrayType mJavaArray;
+ PrimitiveType* mRawArray;
+ size_t mSize;
+ PrimitiveType mBuffer[preallocSize];
+ DISALLOW_COPY_AND_ASSIGN(ScopedArrayRO);
+};
+
+// ScopedNullable***ArrayRO provide convenient read-only access to Java array from JNI code.
+// These accept nullptr. In that case, get() returns nullptr and size() returns 0.
+using ScopedNullableBooleanArrayRO = ScopedArrayRO<jbooleanArray, jboolean, BooleanArrayTraits>;
+using ScopedNullableByteArrayRO = ScopedArrayRO<jbyteArray, jbyte, ByteArrayTraits>;
+using ScopedNullableCharArrayRO = ScopedArrayRO<jcharArray, jchar, CharArrayTraits>;
+using ScopedNullableDoubleArrayRO = ScopedArrayRO<jdoubleArray, jdouble, DoubleArrayTraits>;
+using ScopedNullableFloatArrayRO = ScopedArrayRO<jfloatArray, jfloat, FloatArrayTraits>;
+using ScopedNullableIntArrayRO = ScopedArrayRO<jintArray, jint, IntArrayTraits>;
+using ScopedNullableLongArrayRO = ScopedArrayRO<jlongArray, jlong, LongArrayTraits>;
+using ScopedNullableShortArrayRO = ScopedArrayRO<jshortArray, jshort, ShortArrayTraits>;
+
+} // namespace android
+
+#endif // SCOPED_NULLABLE_PRIMITIVE_ARRAY_H
diff --git a/libs/hwui/jni/text/LineBreaker.cpp b/libs/hwui/jni/text/LineBreaker.cpp
index 02963d8..6986517 100644
--- a/libs/hwui/jni/text/LineBreaker.cpp
+++ b/libs/hwui/jni/text/LineBreaker.cpp
@@ -19,10 +19,9 @@
#include "utils/misc.h"
#include "utils/Log.h"
+#include "graphics_jni_helpers.h"
#include <nativehelper/ScopedStringChars.h>
#include <nativehelper/ScopedPrimitiveArray.h>
-#include <nativehelper/JNIHelp.h>
-#include "core_jni_helpers.h"
#include "scoped_nullable_primitive_array.h"
#include <cstdint>
#include <vector>
diff --git a/libs/hwui/jni/text/MeasuredText.cpp b/libs/hwui/jni/text/MeasuredText.cpp
index e001bd4..7793746 100644
--- a/libs/hwui/jni/text/MeasuredText.cpp
+++ b/libs/hwui/jni/text/MeasuredText.cpp
@@ -22,8 +22,6 @@
#include "utils/Log.h"
#include <nativehelper/ScopedStringChars.h>
#include <nativehelper/ScopedPrimitiveArray.h>
-#include <nativehelper/JNIHelp.h>
-#include "core_jni_helpers.h"
#include <cstdint>
#include <vector>
#include <list>