RenderScript 使应用程序崩溃 "Missing .rs.global_entries"

RenderScript crashes app with "Missing .rs.global_entries"

我正在使用 RenderScript 旋转图像集。应用程序突然崩溃并发出致命信号 11。日志中有 100 条 "D/RenderScript﹕ Missing .rs.global_entries from shared object" 消息。还可以看到显着的内存增加。 这是什么原因?怎么解决的?

我在 android 4.4.2 中使用 Samsung Galaxy 选项卡。我在下面列出了 Gradle、Rs、Java 和 logcat 输出。

compileSdkVersion 23
buildToolsVersion "23.0.2"
defaultConfig {
    minSdkVersion 18
    targetSdkVersion 21
    renderscriptTargetApi 18
    renderscriptSupportModeEnabled true


 #pragma version(1)
#pragma rs_fp_relaxed
#pragma rs java_package_name(com.models)

rs_allocation inImage;
int inWidth;
int inHeight;

uchar4 __attribute__ ((kernel)) rotate_270_clockwise (uchar4 in, uint32_t x, uint32_t y) {
    uint32_t inX  = inWidth - 1 - y;
    uint32_t inY = x;
    const uchar4 *out = rsGetElementAt(inImage, inX, inY);
    return *out;
}

uchar4 __attribute__ ((kernel)) rotate_180_clockwise (uchar4 in, uint32_t x, uint32_t y) {

    uint32_t inX = inWidth - 1 - x;
    uint32_t inY = inHeight - 1 - y;

    const uchar4 *out = rsGetElementAt(inImage, inX, inY);
    return *out;
}

uchar4 __attribute__ ((kernel)) rotate_90_clockwise (uchar4 in, uint32_t x, uint32_t y) {
    uint32_t inX = y;
    uint32_t inY = inHeight - 1 - x;

    const uchar4 *out = rsGetElementAt(inImage, inX, inY);
    return *out;
}

uchar4 __attribute__ ((kernel)) rotate_0_clockwise (uchar4 in, uint32_t x, uint32_t y) {
    uint32_t inX = x;
    uint32_t inY =  y;

    const uchar4 *out = rsGetElementAt(inImage, inX, inY);
    return *out;
}

Java代码

public Bitmap rotateBitmap(Bitmap bitmap, Activity activity, int angle, boolean recycle) {
    Bitmap target = null;
    try {
        RenderScript rs = RenderScript.create(activity, RenderScript.ContextType.DEBUG);
        ScriptC_rotator script = new ScriptC_rotator(rs);
        script.set_inWidth(bitmap.getWidth());
        script.set_inHeight(bitmap.getHeight());
        Allocation sourceAllocation = Allocation.createFromBitmap(rs, bitmap,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        if (recycle)
            bitmap.recycle();
        script.set_inImage(sourceAllocation);

        //270 and 90
        int targetHeight = bitmap.getWidth();
        int targetWidth = bitmap.getHeight();
        if (angle == 180 || angle == 0) {
            targetHeight = bitmap.getHeight();
            targetWidth = bitmap.getWidth();
        }

        Bitmap.Config config = bitmap.getConfig();
        target = Bitmap.createBitmap(targetWidth, targetHeight, config);
        final Allocation targetAllocation = Allocation.createFromBitmap(rs, target,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);
        if (angle == 90)
            script.forEach_rotate_90_clockwise(targetAllocation, targetAllocation);
        else if (angle == 180)
            script.forEach_rotate_180_clockwise(targetAllocation, targetAllocation);
        else if (angle == 270)
            script.forEach_rotate_270_clockwise(targetAllocation, targetAllocation);
        else
            script.forEach_rotate_0_clockwise(targetAllocation, targetAllocation);

        targetAllocation.copyTo(target);
        rs.destroy();
    } catch (Exception e) {
        Log.d("myApp", "rotateBitmap- render script " + e.getMessage());
    }

    return target;
}

日志

 04-19 22:04:02.219  30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
    04-19 22:04:04.259  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.259  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.269  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.269  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.279  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.279  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.289  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:04.319  30377-30377/com.champ D/myApp﹕ processActionUp
    04-19 22:04:04.319  30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
    04-19 22:04:07.389  30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 2335K, 9% free 33300K/36444K, paused 26ms, total 26ms
    04-19 22:04:07.389  30377-30377/com.champ I/dalvikvm-heap﹕ Grow heap (frag case) to 33.607MB for 320336-byte allocation
    04-19 22:04:07.409  30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 3K, 9% free 33609K/36760K, paused 20ms, total 20ms
    04-19 22:04:07.419  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.479  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.489  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.489  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.499  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.509  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.509  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.519  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.529  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.529  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:07.559  30377-30377/com.champ D/myApp﹕ processActionUp
    04-19 22:04:07.559  30377-30377/com.champ D/myApp﹕ processActionUp-scaleDetector.isInProgress()
    04-19 22:04:11.709  30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 1496K, 12% free 32489K/36760K, paused 20ms, total 20ms
    04-19 22:04:11.759  30377-30377/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed 20K, 11% free 32843K/36760K, paused 18ms, total 19ms
    04-19 22:04:11.759  30377-30377/com.champ I/dalvikvm-heap﹕ Grow heap (frag case) to 33.221MB for 383696-byte allocation
    04-19 22:04:11.779  30377-30386/com.champ D/dalvikvm﹕ GC_FOR_ALLOC freed <1K, 11% free 33217K/37136K, paused 16ms, total 17ms
    04-19 22:04:11.779  30377-30761/com.champ D/RenderScript﹕ Missing .rs.global_entries from shared object
    04-19 22:04:11.789  30377-30763/com.champ A/libc﹕ Fatal signal 11 (SIGSEGV) at 0x66082000 (code=1), thread 30763 (AsyncTask #4)

停止创建 RenderScript 上下文 (android.RenderScript),并在每次调用 rotate*() 函数时加载脚本(即 ScriptC_rotator)。您应该缓存这些对象,因为它们都不是轻量级的。这应该减少日志记录,并减少内存碎片(因为您保留 loading/unloading 脚本)。

SIGSEGV 有点令人困惑,但这可能是因为您的 .rs 文件中存在越界内存访问。我看到您正在创建 DEBUG 上下文。当您捕获此日志时,它是否就位,或者是后来添加的东西?考虑到 width/height 由于旋转而交换,我认为您在 .rs 内核中使用了错误的 Y/X 值。考虑使用 rsDebug 打印出您正在写入的索引,您可能会看到它最终失败的地方(并且它确实超出了范围)。