obj->reset();
return reinterpret_cast(obj);
}
Java
层初始化要点如下:
-
Matrix
构造器在native
创建了一个SkMatrix
对象,并通过reinterpret_cast
强制转换为long
赋值给Java
层的native_instance
; -
Matrix
在Java
层其实没有太多操作,真正完成任务的实体是native
层的SkMatrix
。SKMatrix
是 Skia 图形引擎提供的用于完成坐标变换的 3 x 3 矩阵; -
从
Android 8
开始,使用NativeAllocationRegistry
帮助回收 native 层内存。NativeAllocationRegistry
绑定了Java
层和native
层的两个对象,并标记内存大小为 40字节,为什么是 40 个字节呢?我们在源码里寻找答案:SkMatrix.h 、SkMatrix.cpp
# 提示
NativeAllocationRegistry
是用来帮助回收native
层内存的,即当Java
层对象被垃圾回收时,立即去释放Native
层的内存,在Canvas
、Bitmap
等类中也有同样的机制,详见文章:《Android | 带你理解 NativeAllocationRegistry 的原理与设计思想》
3.2 native 层初始化
// SkMatrix.h
SK_BEGIN_REQUIRE_DENSE
class SK_API SkMatrix {
public:
enum {
kMScaleX, //!< horizontal scale factor
kMSkewX, //!< horizontal skew factor
kMTransX, //!< horizontal translation
kMSkewY, //!< vertical skew factor
kMScaleY, //!< vertical scale factor
kMTransY, //!< vertical translation
kMPersp0, //!< input x perspective factor
kMPersp1, //!< input y pers