graphics类是Java最底层的方法,这些方法并不是直接被虚拟机解释执行,而是通过JNI,调用Skia Native库的函数。
JNI函数的具体位置在frameworks\base\core\jni\android\graphics,
基本上一个cpp对应一个graphics类,也有多个graphics类对应一个cpp。
此外GraphicsJNI.h和Graphics.cpp封装了一些获取Java Canvas和Bitmap对象的函数。
让我们看下canvas的JNI对应结构体
static JNINativeMethod gCanvasMethods[] = {
{"finalizer", "(I)V", (void*) SkCanvasGlue::finalizer},
{"initRaster","(I)I", (void*) SkCanvasGlue::initRaster},
{"initGL","()I", (void*) SkCanvasGlue::initGL},
{"isOpaque","()Z", (void*) SkCanvasGlue::isOpaque},
{"getWidth","()I", (void*) SkCanvasGlue::getWidth},
{"getHeight","()I", (void*) SkCanvasGlue::getHeight},
{"native_setBitmap","(II)V", (void*) SkCanvasGlue::setBitmap},
{"nativeSetViewport", "(III)V", (void*) SkCanvasGlue::setViewport},
{"save","()I", (void*) SkCanvasGlue::saveAll},
{"save","(I)I", (void*) SkCanvasGlue::save},
{"native_saveLayer","(ILandroid/graphics/RectF;II)I", (void*) SkCanvasGlue::saveLayer},
{"native_saveLayer","(IFFFFII)I", (void*) SkCanvasGlue::saveLayer4F},
{"native_saveLayerAlpha","(ILandroid/graphics/RectF;II)I", (void*) SkCanvasGlue::saveLayerAlpha},
{"native_saveLayerAlpha","(IFFFFII)I", (void*) SkCanvasGlue::saveLayerAlpha4F},
{"restore","()V", (void*) SkCanvasGlue::restore},
{"getSaveCount","()I", (void*) SkCanvasGlue::getSaveCount},
{"restoreToCount","(I)V", (void*) SkCanvasGlue::restoreToCount},
{"translate","(FF)V", (void*) SkCanvasGlue::translate},
{"scale","(FF)V", (void*) SkCanvasGlue::scale__FF},
{"rotate","(F)V", (void*) SkCanvasGlue::rotate__F},
{"skew","(FF)V", (void*) SkCanvasGlue::skew__FF},
... ...省略
举个rotate的例子,在canvas.java中定义了rotate为native,同时还有一个重载的rotate供上层调用:
/**
* Preconcat the current matrix with the specified rotation.
*
* @param degrees The amount to rotate, in degrees
*/
public native void rotate(float degrees);
/**
* Preconcat the current matrix with the specified rotation.
*
* @param degrees The amount to rotate, in degrees
* @param px The x-coord for the pivot point (unchanged by the rotation)
* @param py The y-coord for the pivot point (unchanged by the rotation)
*/
public final void rotate(float degrees, float px, float py) {
translate(px, py);
rotate(degrees);
translate(-px, -py);
}
JNI转换表该项
{"rotate","(F)V", (void*) SkCanvasGlue::rotate__F},
应用层调用rotate方法经过JNI转换,调用到框架中的SkCanvasGlue::rotate__F函数。
而位于canvas.cpp的class SkCanvasGlue类封装了JNI转换后的函数,比如rotate__F。
static void rotate__F(JNIEnv* env, jobject jcanvas, jfloat degrees) {
NPE_CHECK_RETURN_VOID(env, jcanvas);
SkScalar degrees_ = SkFloatToScalar(degrees);
(void)GraphicsJNI::getNativeCanvas(env, jcanvas)->rotate(degrees_);
}
(void)GraphicsJNI::getNativeCanvas(env, jcanvas)返回指向SkCanvas结构的指针,即调用SkCanvas中rotate函数。
SkCanvas属于Skia库,接下来要看看Skia库的结构。