我正在使用 RenderScript 来旋转图像集。应用程序突然崩溃,并出现致命信号 11。日志中有数百条“D/RenderScript:共享对象中缺少 .rs.global_entries”消息。还可以看到内存显着增加。 这是什么原因呢?怎么解决?
我正在使用 Android 4.4.2 的 Samsung Galexy 选项卡。我在下面列出了 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)。您应该缓存这些对象,因为它们使用起来也不是轻量级的。这应该减少日志记录,并减少内存 fragment (因为您不断加载/卸载脚本)。
SIGSEGV 有点令人困惑,但这可能是因为 .rs 文件中的内存访问越界。我看到您正在创建一个调试上下文。当您捕获此日志时,该信息是否已就位,或者是稍后添加的内容?考虑到由于旋转而交换宽度/高度,我认为您在 .rs 内核中使用了错误的 Y/X 值。考虑使用 rsDebug 打印出您正在写入的索引,您可能会看到它最终在哪里失败(并且它确实超出了范围)。
关于java - RenderScript 使应用程序崩溃,错误代码为 "Missing .rs.global_entries",我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36718089/