Android-在Renderscript中调用多个内核函数

时间:2019-05-29 12:00:34

标签: android renderscript single-source

我想使用Renderscript逐个启动2个内核函数。因此,在文档中,我找到了单源渲染脚本(https://developer.android.com/guide/topics/renderscript/compute)的概念。之后,我实现了以下代码:

import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.renderscript.Allocation;
import android.renderscript.RenderScript;
import android.support.v7.app.AppCompatActivity;
import android.os.Bundle;
import android.view.View;
import android.widget.Button;
import android.widget.ImageView;

public class MainActivity extends AppCompatActivity {

    private Bitmap mBitmapIn;
    private Bitmap mBitmapOut;
    private ImageView mImageView;
    private Button mButton;
    private Allocation mInAllocation;
    private Allocation mOutAllocation;

    private ScriptC_single mScript;
    private RenderScriptTask mCurrentTask;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_main);

        // Initialize UI
        mBitmapIn = loadBitmap(R.drawable.data);

        mBitmapOut =  Bitmap.createBitmap
                (
                        mBitmapIn.getWidth(),
                        mBitmapIn.getHeight(),
                        mBitmapIn.getConfig()
                );


        mImageView = (ImageView) findViewById(R.id.imageView);
        mImageView.setImageBitmap(mBitmapOut);

        mButton = (Button) findViewById(R.id.button);

        RenderScript rs = RenderScript.create(this);
        mInAllocation = Allocation.createFromBitmap(rs, mBitmapIn);
        mOutAllocation = Allocation.createFromBitmap(rs, mBitmapOut);
        mScript = new ScriptC_single(rs);


        mButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {

                mScript.invoke_process(mInAllocation, mOutAllocation);
                mOutAllocation.copyTo(mBitmapOut);
                mImageView.setImageBitmap(mBitmapOut);
                mImageView.invalidate();
            }
        });
    }


    /**
     * Helper to load Bitmap from resource
     */
    private Bitmap loadBitmap(int resource) {
        final BitmapFactory.Options options = new BitmapFactory.Options();
        options.inPreferredConfig = Bitmap.Config.ARGB_8888;
        return BitmapFactory.decodeResource(getResources(), resource, options);
    }
}

renderscript文件如下所示:

#pragma version(1)
#pragma rs java_package_name(com.celik.abdullah.playingwithsaturation)
#pragma rs_fp_relaxed

uchar4 __attribute__((kernel)) invert(uchar4 in){
    uchar4 out = in;

    out.r = 255 - in.r;
    out.g = 255 - in.g;
    out.b = 255 - in.b;

    return out;
}

uchar4 __attribute__((kernel)) greyscale(uchar4 in) {

  uchar grayscale = in.r * 0.299 + in.g * 0.587 + in.b * 0.114;

  uchar4 pixelOut;

  pixelOut.a = in.a;
  pixelOut.r = grayscale;
  pixelOut.g = grayscale;
  pixelOut.b = grayscale;

  return pixelOut;
}

void process(rs_allocation inputImage, rs_allocation outputImage) {
  const uint32_t imageWidth = rsAllocationGetDimX(inputImage);
  const uint32_t imageHeight = rsAllocationGetDimY(inputImage);
  rs_allocation tmp = rsCreateAllocation_uchar4(imageWidth, imageHeight);
  rsForEach(invert, inputImage, tmp);
  rsForEach(greyscale, tmp, outputImage);
}

因此,在上面的代码中,我们加载数据(简单图像),对其进行一些操作,然后将结果呈现回ImageView。 操作是反转和灰度。我分别测试了它们,因此我确定它们可以工作。

但是当我开始运行项目时,出现以下错误:

  

处理'命令   '/home/ac3l1k/Android/Sdk/build-tools/27.0.3/llvm-rs-cc''完成   具有非零退出值1的

为什么?有人可以帮忙吗?

0 个答案:

没有答案