首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >Renderscript致命信号11 (代码= 1)

Renderscript致命信号11 (代码= 1)
EN

Stack Overflow用户
提问于 2014-06-30 20:30:13
回答 2查看 1.1K关注 0票数 1

在我的项目中,我采取了一个15x15随机颜色的位图,并确定了每个像素之间的区别。

当调用Distinct.start()时,我得到以下错误:

代码语言:javascript
复制
A/libc(18416): Fatal signal 11 (SIGSEGV) at 0x00000038 (code=1), thread 18538 (AsyncTask #1)

从我所能找到的情况来看,信号11表明我试图非法访问内存,但我找不到它可能在哪里。除了结果的实例化和返回之外,我甚至尝试删除脚本中的所有内容。

distinct.rs:

代码语言:javascript
复制
#pragma version(1)
#pragma rs java_package_name(com.moffitj.horizon)
#pragma rs_fp_relaxed

rs_allocation image;
int imgSize;
int side;


float __attribute__((kernel)) root(float in, uint32_t x, uint32_t y){

    //where we'll store the cumulative distance for the current pixel
    float result = 0.0f;

    //Pull the current pixel and unpack it's componants
    const uchar4 current = *(const uchar4*)rsGetElementAt(image, (y * side) + x, 0);
    float4 currentPixel = rsUnpackColor8888(current);

    //find how distinct the current pixel is from all others (and self, to save a branch)
    for(int i = 0; i < imgSize; i++){

        //get the next pixel and uppack it
        const uchar4 pixel = *(const uchar4*)rsGetElementAt(image, i, 0);
        float4 unpackedPixel = rsUnpackColor8888(pixel);

        //compare the distance between the two pixels
        float4 temp = sqrt(pow(currentPixel - unpackedPixel,2));

        //add the difference to the running total
        result += (temp.w + temp.x + temp.y + temp.z);
    }

    result /= imgSize;

    return result;

}

distinct.java:

代码语言:javascript
复制
public class Distinct {

    private Allocation mBitmapAllocation;
    private Allocation mReferenceAllocation;
    private Allocation mResultAllocation;
    private int mImgSize;
    private int mNumBlocks;
    private RenderScript mRS;
    private Context mContext;
    private ScriptC_distinct mScript;
    private Bitmap mBitmap;

    /**
     * 
     * @param bitmap
     * @throws IllegalArgumentException if the bitmap is not square or divisible by 64
     */
    public Distinct(Bitmap bitmap, Context context) throws IllegalArgumentException {

        mBitmap = bitmap;
        mContext = context;
        mImgSize = bitmap.getHeight() * bitmap.getWidth();
        mRS = RenderScript.create(mContext);
        mBitmapAllocation = Allocation.createFromBitmap(mRS, bitmap, Allocation.MipmapControl.MIPMAP_NONE, Allocation.USAGE_SCRIPT);

        Type.Builder referenceBuilder = new Type.Builder(mRS, Element.F32(mRS));//Dummy data won't be used, just to get location once inside script
        referenceBuilder.setX(bitmap.getWidth());
        referenceBuilder.setY(bitmap.getHeight());
        Type type = referenceBuilder.create();

        mReferenceAllocation = Allocation.createTyped(mRS, type, Allocation.USAGE_SCRIPT);

        Type.Builder resultBuilder = new Type.Builder(mRS, Element.F32(mRS));
        resultBuilder.setX(bitmap.getWidth());
        resultBuilder.setY(bitmap.getHeight());
        Type resultType = resultBuilder.create();

        mResultAllocation = Allocation.createTyped(mRS, resultType, Allocation.USAGE_SCRIPT);       

        /*Type.Builder imgTypeBuilder = new Type.Builder(mRS, Element.I32(mRS));
        imgTypeBuilder.setX(mImgSize * mImgSize);
        imgTypeBuilder.setY(1);
        Type imgType = imgTypeBuilder.create();

        mBitmapAllocation = Allocation.createTyped(mRS, imgType, Allocation.USAGE_SCRIPT);*/

        mScript = new ScriptC_distinct(mRS, mContext.getResources(), R.raw.average);
        mScript.set_image(mBitmapAllocation);
        mScript.set_imgSize(mImgSize);
        mScript.set_side(bitmap.getHeight());

    }

    public void start(){

        mScript.forEach_root(mReferenceAllocation, mResultAllocation);

    }
EN

回答 2

Stack Overflow用户

回答已采纳

发布于 2014-07-20 00:13:55

在匆忙中,我从另一个项目复制了一个工作解决方案,错过了更新一行。

代码语言:javascript
复制
 mScript = new ScriptC_distinct(mRS, mContext.getResources(), R.raw.average);

应该改到

代码语言:javascript
复制
mScript = new ScriptC_distinct(mRS, mContext.getResources(), R.raw.distinct);
票数 0
EN

Stack Overflow用户

发布于 2014-06-30 20:51:55

您的错误出现在:

代码语言:javascript
复制
rsGetElementAt(image, (y * side) + x, 0);

假设位图的布局允许以这种方式对其进行索引,但这实际上是未定义的行为。您应该为rsGetElementAt()的x和y部分使用适当的索引。我还建议您切换到更高性能的rsGetElementAt_uchar4()函数(对于所有原始/向量类型都存在这样的帮助)。此函数将直接返回一个uchar4,因此不需要转换指针并取消引用它。

我的另一个建议是尝试使用调试运行时来识别这些边界错误。只需将标志添加到上面的create()调用:

代码语言:javascript
复制
RenderScript.create(mContext, RenderScript.ContextType.DEBUG);

错误/警告将出现在亚行logcat下。

票数 4
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/24498447

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档