cleanup so that the Paint.h is only accessed via its nativePtr not a JNI lookup
bug: 17641888
Change-Id: I8fc2a01fdcf62dd33b443b0a2302df5e29dc3f49
diff --git a/core/jni/android/graphics/Graphics.cpp b/core/jni/android/graphics/Graphics.cpp
index d7b75db..f0585a3 100644
--- a/core/jni/android/graphics/Graphics.cpp
+++ b/core/jni/android/graphics/Graphics.cpp
@@ -166,10 +166,6 @@
static jclass gCanvas_class;
static jfieldID gCanvas_nativeInstanceID;
-static jclass gPaint_class;
-static jfieldID gPaint_nativeInstanceID;
-static jfieldID gPaint_nativeTypefaceID;
-
static jclass gPicture_class;
static jfieldID gPicture_nativeInstanceID;
@@ -370,25 +366,6 @@
return c;
}
-android::Paint* GraphicsJNI::getNativePaint(JNIEnv* env, jobject paint) {
- SkASSERT(env);
- SkASSERT(paint);
- SkASSERT(env->IsInstanceOf(paint, gPaint_class));
- jlong paintHandle = env->GetLongField(paint, gPaint_nativeInstanceID);
- android::Paint* p = reinterpret_cast<android::Paint*>(paintHandle);
- SkASSERT(p);
- return p;
-}
-
-android::TypefaceImpl* GraphicsJNI::getNativeTypeface(JNIEnv* env, jobject paint) {
- SkASSERT(env);
- SkASSERT(paint);
- SkASSERT(env->IsInstanceOf(paint, gPaint_class));
- jlong typefaceHandle = env->GetLongField(paint, gPaint_nativeTypefaceID);
- android::TypefaceImpl* p = reinterpret_cast<android::TypefaceImpl*>(typefaceHandle);
- return p;
-}
-
SkRegion* GraphicsJNI::getNativeRegion(JNIEnv* env, jobject region)
{
SkASSERT(env);
@@ -728,10 +705,6 @@
gCanvas_class = make_globalref(env, "android/graphics/Canvas");
gCanvas_nativeInstanceID = getFieldIDCheck(env, gCanvas_class, "mNativeCanvasWrapper", "J");
- gPaint_class = make_globalref(env, "android/graphics/Paint");
- gPaint_nativeInstanceID = getFieldIDCheck(env, gPaint_class, "mNativePaint", "J");
- gPaint_nativeTypefaceID = getFieldIDCheck(env, gPaint_class, "mNativeTypeface", "J");
-
gPicture_class = make_globalref(env, "android/graphics/Picture");
gPicture_nativeInstanceID = getFieldIDCheck(env, gPicture_class, "mNativePicture", "J");
diff --git a/core/jni/android/graphics/GraphicsJNI.h b/core/jni/android/graphics/GraphicsJNI.h
index dcc97e5..8ab4660 100644
--- a/core/jni/android/graphics/GraphicsJNI.h
+++ b/core/jni/android/graphics/GraphicsJNI.h
@@ -48,8 +48,6 @@
static void point_to_jpointf(const SkPoint& point, JNIEnv*, jobject jpointf);
static SkCanvas* getNativeCanvas(JNIEnv*, jobject canvas);
- static android::Paint* getNativePaint(JNIEnv*, jobject paint);
- static android::TypefaceImpl* getNativeTypeface(JNIEnv*, jobject paint);
static SkBitmap* getNativeBitmap(JNIEnv*, jobject bitmap);
static SkRegion* getNativeRegion(JNIEnv*, jobject region);
diff --git a/core/jni/android/graphics/Movie.cpp b/core/jni/android/graphics/Movie.cpp
index 226f83e..c0c835f 100644
--- a/core/jni/android/graphics/Movie.cpp
+++ b/core/jni/android/graphics/Movie.cpp
@@ -68,7 +68,7 @@
}
static void movie_draw(JNIEnv* env, jobject movie, jobject canvas,
- jfloat fx, jfloat fy, jobject jpaint) {
+ jfloat fx, jfloat fy, jlong paintHandle) {
NPE_CHECK_RETURN_VOID(env, movie);
NPE_CHECK_RETURN_VOID(env, canvas);
// its OK for paint to be null
@@ -76,7 +76,7 @@
SkMovie* m = J2Movie(env, movie);
SkCanvas* c = GraphicsJNI::getNativeCanvas(env, canvas);
const SkBitmap& b = m->bitmap();
- const SkPaint* p = jpaint ? GraphicsJNI::getNativePaint(env, jpaint) : NULL;
+ const android::Paint* p = reinterpret_cast<android::Paint*>(paintHandle);
c->drawBitmap(b, fx, fy, p);
}
@@ -146,7 +146,7 @@
{ "isOpaque", "()Z", (void*)movie_isOpaque },
{ "duration", "()I", (void*)movie_duration },
{ "setTime", "(I)Z", (void*)movie_setTime },
- { "draw", "(Landroid/graphics/Canvas;FFLandroid/graphics/Paint;)V",
+ { "nDraw", "(Landroid/graphics/Canvas;FFJ)V",
(void*)movie_draw },
{ "nativeDecodeAsset", "(J)Landroid/graphics/Movie;",
(void*)movie_decodeAsset },
diff --git a/core/jni/android/graphics/Paint.cpp b/core/jni/android/graphics/Paint.cpp
index 4bb31fc..8bed7e9 100644
--- a/core/jni/android/graphics/Paint.cpp
+++ b/core/jni/android/graphics/Paint.cpp
@@ -61,6 +61,10 @@
static jclass gFontMetricsInt_class;
static JMetricsID gFontMetricsInt_fieldID;
+static jclass gPaint_class;
+static jfieldID gPaint_nativeInstanceID;
+static jfieldID gPaint_nativeTypefaceID;
+
static void defaultSettingsForAndroid(Paint* paint) {
// GlyphID encoding is required because we are using Harfbuzz shaping
paint->setTextEncoding(Paint::kGlyphID_TextEncoding);
@@ -72,6 +76,25 @@
AFTER, AT_OR_AFTER, BEFORE, AT_OR_BEFORE, AT
};
+ static Paint* getNativePaint(JNIEnv* env, jobject paint) {
+ SkASSERT(env);
+ SkASSERT(paint);
+ SkASSERT(env->IsInstanceOf(paint, gPaint_class));
+ jlong paintHandle = env->GetLongField(paint, gPaint_nativeInstanceID);
+ android::Paint* p = reinterpret_cast<android::Paint*>(paintHandle);
+ SkASSERT(p);
+ return p;
+ }
+
+ static TypefaceImpl* getNativeTypeface(JNIEnv* env, jobject paint) {
+ SkASSERT(env);
+ SkASSERT(paint);
+ SkASSERT(env->IsInstanceOf(paint, gPaint_class));
+ jlong typefaceHandle = env->GetLongField(paint, gPaint_nativeTypefaceID);
+ android::TypefaceImpl* p = reinterpret_cast<android::TypefaceImpl*>(typefaceHandle);
+ return p;
+ }
+
static void finalizer(JNIEnv* env, jobject clazz, jlong objHandle) {
Paint* obj = reinterpret_cast<Paint*>(objHandle);
delete obj;
@@ -106,7 +129,7 @@
static jint getFlags(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- Paint* nativePaint = GraphicsJNI::getNativePaint(env, paint);
+ Paint* nativePaint = getNativePaint(env, paint);
uint32_t result = nativePaint->getFlags();
result &= ~sFilterBitmapFlag; // Filtering no longer stored in this bit. Mask away.
if (nativePaint->getFilterLevel() != Paint::kNone_FilterLevel) {
@@ -117,7 +140,7 @@
static void setFlags(JNIEnv* env, jobject paint, jint flags) {
NPE_CHECK_RETURN_VOID(env, paint);
- Paint* nativePaint = GraphicsJNI::getNativePaint(env, paint);
+ Paint* nativePaint = getNativePaint(env, paint);
// Instead of modifying 0x02, change the filter level.
nativePaint->setFilterLevel(flags & sFilterBitmapFlag
? Paint::kLow_FilterLevel
@@ -132,55 +155,55 @@
static jint getHinting(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return GraphicsJNI::getNativePaint(env, paint)->getHinting()
+ return getNativePaint(env, paint)->getHinting()
== Paint::kNo_Hinting ? 0 : 1;
}
static void setHinting(JNIEnv* env, jobject paint, jint mode) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setHinting(
+ getNativePaint(env, paint)->setHinting(
mode == 0 ? Paint::kNo_Hinting : Paint::kNormal_Hinting);
}
static void setAntiAlias(JNIEnv* env, jobject paint, jboolean aa) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setAntiAlias(aa);
+ getNativePaint(env, paint)->setAntiAlias(aa);
}
static void setLinearText(JNIEnv* env, jobject paint, jboolean linearText) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setLinearText(linearText);
+ getNativePaint(env, paint)->setLinearText(linearText);
}
static void setSubpixelText(JNIEnv* env, jobject paint, jboolean subpixelText) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setSubpixelText(subpixelText);
+ getNativePaint(env, paint)->setSubpixelText(subpixelText);
}
static void setUnderlineText(JNIEnv* env, jobject paint, jboolean underlineText) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setUnderlineText(underlineText);
+ getNativePaint(env, paint)->setUnderlineText(underlineText);
}
static void setStrikeThruText(JNIEnv* env, jobject paint, jboolean strikeThruText) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setStrikeThruText(strikeThruText);
+ getNativePaint(env, paint)->setStrikeThruText(strikeThruText);
}
static void setFakeBoldText(JNIEnv* env, jobject paint, jboolean fakeBoldText) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setFakeBoldText(fakeBoldText);
+ getNativePaint(env, paint)->setFakeBoldText(fakeBoldText);
}
static void setFilterBitmap(JNIEnv* env, jobject paint, jboolean filterBitmap) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setFilterLevel(
+ getNativePaint(env, paint)->setFilterLevel(
filterBitmap ? Paint::kLow_FilterLevel : Paint::kNone_FilterLevel);
}
static void setDither(JNIEnv* env, jobject paint, jboolean dither) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setDither(dither);
+ getNativePaint(env, paint)->setDither(dither);
}
static jint getStyle(JNIEnv* env, jobject clazz,jlong objHandle) {
@@ -197,45 +220,45 @@
static jint getColor(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
int color;
- color = GraphicsJNI::getNativePaint(env, paint)->getColor();
+ color = getNativePaint(env, paint)->getColor();
return static_cast<jint>(color);
}
static jint getAlpha(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
int alpha;
- alpha = GraphicsJNI::getNativePaint(env, paint)->getAlpha();
+ alpha = getNativePaint(env, paint)->getAlpha();
return static_cast<jint>(alpha);
}
static void setColor(JNIEnv* env, jobject paint, jint color) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setColor(color);
+ getNativePaint(env, paint)->setColor(color);
}
static void setAlpha(JNIEnv* env, jobject paint, jint a) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setAlpha(a);
+ getNativePaint(env, paint)->setAlpha(a);
}
static jfloat getStrokeWidth(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeWidth());
+ return SkScalarToFloat(getNativePaint(env, paint)->getStrokeWidth());
}
static void setStrokeWidth(JNIEnv* env, jobject paint, jfloat width) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setStrokeWidth(width);
+ getNativePaint(env, paint)->setStrokeWidth(width);
}
static jfloat getStrokeMiter(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getStrokeMiter());
+ return SkScalarToFloat(getNativePaint(env, paint)->getStrokeMiter());
}
static void setStrokeMiter(JNIEnv* env, jobject paint, jfloat miter) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setStrokeMiter(miter);
+ getNativePaint(env, paint)->setStrokeMiter(miter);
}
static jint getStrokeCap(JNIEnv* env, jobject clazz, jlong objHandle) {
@@ -370,44 +393,44 @@
static jboolean isElegantTextHeight(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- Paint* obj = GraphicsJNI::getNativePaint(env, paint);
+ Paint* obj = getNativePaint(env, paint);
return obj->getFontVariant() == VARIANT_ELEGANT;
}
static void setElegantTextHeight(JNIEnv* env, jobject paint, jboolean aa) {
NPE_CHECK_RETURN_VOID(env, paint);
- Paint* obj = GraphicsJNI::getNativePaint(env, paint);
+ Paint* obj = getNativePaint(env, paint);
obj->setFontVariant(aa ? VARIANT_ELEGANT : VARIANT_DEFAULT);
}
static jfloat getTextSize(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSize());
+ return SkScalarToFloat(getNativePaint(env, paint)->getTextSize());
}
static void setTextSize(JNIEnv* env, jobject paint, jfloat textSize) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setTextSize(textSize);
+ getNativePaint(env, paint)->setTextSize(textSize);
}
static jfloat getTextScaleX(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextScaleX());
+ return SkScalarToFloat(getNativePaint(env, paint)->getTextScaleX());
}
static void setTextScaleX(JNIEnv* env, jobject paint, jfloat scaleX) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setTextScaleX(scaleX);
+ getNativePaint(env, paint)->setTextScaleX(scaleX);
}
static jfloat getTextSkewX(JNIEnv* env, jobject paint) {
NPE_CHECK_RETURN_ZERO(env, paint);
- return SkScalarToFloat(GraphicsJNI::getNativePaint(env, paint)->getTextSkewX());
+ return SkScalarToFloat(getNativePaint(env, paint)->getTextSkewX());
}
static void setTextSkewX(JNIEnv* env, jobject paint, jfloat skewX) {
NPE_CHECK_RETURN_VOID(env, paint);
- GraphicsJNI::getNativePaint(env, paint)->setTextSkewX(skewX);
+ getNativePaint(env, paint)->setTextSkewX(skewX);
}
static jfloat getLetterSpacing(JNIEnv* env, jobject clazz, jlong paintHandle) {
@@ -436,8 +459,8 @@
const int kElegantAscent = 1900;
const int kElegantDescent = -500;
const int kElegantLeading = 0;
- Paint* paint = GraphicsJNI::getNativePaint(env, jpaint);
- TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
+ Paint* paint = getNativePaint(env, jpaint);
+ TypefaceImpl* typeface = getNativeTypeface(env, jpaint);
typeface = TypefaceImpl_resolveDefault(typeface);
FakedFont baseFont = typeface->fFontCollection->baseFontFaked(typeface->fStyle);
float saveSkewX = paint->getTextSkewX();
@@ -525,12 +548,12 @@
return 0;
}
- Paint* paint = GraphicsJNI::getNativePaint(env, jpaint);
+ Paint* paint = getNativePaint(env, jpaint);
const jchar* textArray = env->GetCharArrayElements(text, NULL);
jfloat result = 0;
Layout layout;
- TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
+ TypefaceImpl* typeface = getNativeTypeface(env, jpaint);
MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, index, count, textLength);
result = layout.getAdvance();
env->ReleaseCharArrayElements(text, const_cast<jchar*>(textArray), JNI_ABORT);
@@ -553,11 +576,11 @@
}
const jchar* textArray = env->GetStringChars(text, NULL);
- Paint* paint = GraphicsJNI::getNativePaint(env, jpaint);
+ Paint* paint = getNativePaint(env, jpaint);
jfloat width = 0;
Layout layout;
- TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
+ TypefaceImpl* typeface = getNativeTypeface(env, jpaint);
MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, start, count, textLength);
width = layout.getAdvance();
@@ -575,11 +598,11 @@
}
const jchar* textArray = env->GetStringChars(text, NULL);
- Paint* paint = GraphicsJNI::getNativePaint(env, jpaint);
+ Paint* paint = getNativePaint(env, jpaint);
jfloat width = 0;
Layout layout;
- TypefaceImpl* typeface = GraphicsJNI::getNativeTypeface(env, jpaint);
+ TypefaceImpl* typeface = getNativeTypeface(env, jpaint);
MinikinUtils::doLayout(&layout, paint, bidiFlags, typeface, textArray, 0, textLength, textLength);
width = layout.getAdvance();
@@ -1018,32 +1041,37 @@
{"native_hasShadowLayer", "(J)Z", (void*)PaintGlue::hasShadowLayer}
};
+static jclass makeGlobalRef(JNIEnv* env, const char classname[])
+{
+ jclass c = env->FindClass(classname);
+ SkASSERT(c);
+ return (jclass) env->NewGlobalRef(c);
+}
+
static jfieldID req_fieldID(jfieldID id) {
SkASSERT(id);
return id;
}
int register_android_graphics_Paint(JNIEnv* env) {
- gFontMetrics_class = env->FindClass("android/graphics/Paint$FontMetrics");
- SkASSERT(gFontMetrics_class);
- gFontMetrics_class = (jclass)env->NewGlobalRef(gFontMetrics_class);
-
+ gFontMetrics_class = makeGlobalRef(env, "android/graphics/Paint$FontMetrics");
gFontMetrics_fieldID.top = req_fieldID(env->GetFieldID(gFontMetrics_class, "top", "F"));
gFontMetrics_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetrics_class, "ascent", "F"));
gFontMetrics_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetrics_class, "descent", "F"));
gFontMetrics_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetrics_class, "bottom", "F"));
gFontMetrics_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetrics_class, "leading", "F"));
- gFontMetricsInt_class = env->FindClass("android/graphics/Paint$FontMetricsInt");
- SkASSERT(gFontMetricsInt_class);
- gFontMetricsInt_class = (jclass)env->NewGlobalRef(gFontMetricsInt_class);
-
+ gFontMetricsInt_class = makeGlobalRef(env, "android/graphics/Paint$FontMetricsInt");
gFontMetricsInt_fieldID.top = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "top", "I"));
gFontMetricsInt_fieldID.ascent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "ascent", "I"));
gFontMetricsInt_fieldID.descent = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "descent", "I"));
gFontMetricsInt_fieldID.bottom = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "bottom", "I"));
gFontMetricsInt_fieldID.leading = req_fieldID(env->GetFieldID(gFontMetricsInt_class, "leading", "I"));
+ gPaint_class = makeGlobalRef(env, "android/graphics/Paint");
+ gPaint_nativeInstanceID = req_fieldID(env->GetFieldID(gPaint_class, "mNativePaint", "J"));
+ gPaint_nativeTypefaceID = req_fieldID(env->GetFieldID(gPaint_class, "mNativeTypeface", "J"));
+
int result = AndroidRuntime::registerNativeMethods(env, "android/graphics/Paint", methods,
sizeof(methods) / sizeof(methods[0]));
return result;
diff --git a/graphics/java/android/graphics/Movie.java b/graphics/java/android/graphics/Movie.java
index b0a4553..ba37340 100644
--- a/graphics/java/android/graphics/Movie.java
+++ b/graphics/java/android/graphics/Movie.java
@@ -35,12 +35,16 @@
public native boolean isOpaque();
public native int duration();
- public native boolean setTime(int relativeMilliseconds);
+ public native boolean setTime(int relativeMilliseconds);
- public native void draw(Canvas canvas, float x, float y, Paint paint);
-
+ private native void nDraw(Canvas canvas, float x, float y, long paintHandle);
+
+ public void draw(Canvas canvas, float x, float y, Paint paint) {
+ nDraw(canvas, x, y, paint != null ? paint.mNativePaint : 0);
+ }
+
public void draw(Canvas canvas, float x, float y) {
- draw(canvas, x, y, null);
+ nDraw(canvas, x, y, 0);
}
public static Movie decodeStream(InputStream is) {