简体   繁体   中英

RenderScript acting weird and returning nothing

I'm using renderscript and when i write a kernel, at first it is acting ok. but when i change the kernel and rebuild the project it returns nothing. anybody knows why? It just return a blank photo and nothing else.

I have fixed it. the problem for me was targetSdkVersion 24 . i found out that in android 7, there has been a lot of changes (NEW FEATURES) for renderscript, so i have changed it to targetSdkVersion 22 and it fixed the issue. I tried to do a simple filter, and after using the filter it showed me nothing in the imageview. But when i changed the targetSdkVersion to 22 it fixed the problem and i could see the filtered image in the imageview. here is the code i've used:

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

uchar4 out = in;
 out.r = 255 - in.r;
 out.g = 255 - in.g;
 out.b = 255 - in.b;
 return out;
}

if you are interested you can try it with the targetSdkVersion 24 and 22. and here is the code for java part:

public void imageFilter(Bitmap bmp) {

    doAllocation();

    operationBitmap = Bitmap.createBitmap(bmp.getWidth(),
            bmp.getHeight(), bmp.getConfig());

    int height = operationBitmap.getHeight();
    int width = operationBitmap.getWidth();

    Toast.makeText(MainActivity.this, width + " x " + height, Toast.LENGTH_SHORT).show();

    ScriptC_cnn cnnScript = new ScriptC_cnn(rs);
    cnnScript.forEach_filtered(allocIn, allocOut);

    allocOut.copyTo(operationBitmap);
    selectedImagePreview.setImageBitmap(operationBitmap);
    rs.destroy();
}

public void doAllocation() {

        allocIn = Allocation.createFromBitmap(rs, originalBitmap,
                Allocation.MipmapControl.MIPMAP_NONE,
                Allocation.USAGE_SCRIPT);

        allocOut = Allocation.createTyped(rs,
                allocIn.getType());
}

and you can see the renewed document of google in the developer.android website. here is the link: https://developer.android.com/guide/topics/renderscript/compute.html#writing-an-rs-kernel

you can see that for a mapping kernel you can use RS_KERNEL instead of attribute((kernel)). and as google mentioned RS_KERNEL is a macro defined automatically by RenderScript for your convenience:

#define RS_KERNEL __attribute__((kernel))

UPDATE Here is my gradle:

android {
compileSdkVersion 24
buildToolsVersion "24.0.1"

defaultConfig {
    applicationId "shahryar.com.cellularneuralnetwork"
    minSdkVersion 16
    targetSdkVersion 22
    versionCode 1
    versionName "1.0"
    renderscriptSupportModeEnabled true;
    renderscriptTargetApi 16;
}
buildTypes {
    release {
        minifyEnabled false
        proguardFiles getDefaultProguardFile('proguard-android.txt'), 'proguard-rules.pro'
    }
}
}

dependencies {
compile fileTree(dir: 'libs', include: ['*.jar'])
testCompile 'junit:junit:4.12'
compile 'com.android.support:appcompat-v7:24.2.0'
}

And i have used renderscriptTargetApi 16 according this line in the google's document: Valid values for this setting are any integer value from 11 to the most recently released API level. If your minimum SDK version specified in your application manifest is set to a different value, that value is ignored and the target value in the build file is used to set the minimum SDK version.

Note : this is the gradle version which is working now for me.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM