从Android内核中拆分一些JNI c++代码

Unraveling some JNI C++ code from the Android core

本文关键字:JNI c++ 代码 拆分 Android 内核      更新时间:2023-10-16

我对c++和JNI不是很有经验,所以我对这段代码的某些部分有真正的麻烦(这是Android框架的一部分,更准确地说来自Surface类的CPP源):

static void Surface_unlockCanvasAndPost(
        JNIEnv* env, jobject clazz, jobject argCanvas)
{
    jobject canvas = env->GetObjectField(clazz, so.canvas);
    if (canvas != argCanvas) {
        doThrow(env, "java/lang/IllegalArgumentException", NULL);
        return;
    }
    const sp<Surface>& surface(getSurface(env, clazz));
    if (!Surface::isValid(surface))
        return;
    // detach the canvas from the surface
    SkCanvas* nativeCanvas =
        (SkCanvas*) env->GetIntField(canvas, no.native_canvas);
    int saveCount = env->GetIntField(clazz, so.saveCount);
    nativeCanvas->restoreToCount(saveCount);
    nativeCanvas->setBitmapDevice(SkBitmap());
    env->SetIntField(clazz, so.saveCount, 0);
    // unlock surface
    status_t err = surface->unlockAndPost();
    if (err < 0) {
        doThrow(env, "java/lang/IllegalArgumentException", NULL);
    }
}

特别让我烦恼的是:

const sp<Surface>& surface(getSurface(env, clazz));

这让我很困惑。没有等号,这些模板让人更难理解。

谁能帮我弄清楚这行代码?

这是引用的构造函数调用语法-这样写有点模糊,就像写int i(23);而不是int i = 23;一样,但它实际上相当于写const sp<Surface>& surface = getSurface(env, clazz); .