带有渲染脚本的像素排序

问题描述 投票:0回答:1

我用纯Java制作了一个小像素排序应用程序,它可以正常工作,但是性能很差。我听说renderscript仅用于此!

我做了一些代码,但是C99太新了,所以我知道有些东西丢失了。我做了这个小测试脚本。

#pragma version(1)
#pragma rs java_package_name(com.simahero.pixelsort)
#pragma rs_fp_relaxed

float treshhold = 0.f;

static void swap(uchar4 *xp, uchar4 *yp)
{
    uchar4 temp = *xp;
    *xp = *yp;
    *yp = temp;
}

static void selectionSort(uchar4 arr[], int n)
{
    int i, j, min_idx;

    for (i = 0; i < n-1; i++)
    {
        min_idx = i;
        for (j = i+1; j < n; j++)
        if (arr[j].r < arr[min_idx].r)
            min_idx = j;

        swap(&arr[min_idx], &arr[i]);
    }
}

rs_allocation RS_KERNEL invert(rs_allocation in) {

    for (int i = 0; i < rsAllocationGetDimY(in); i++){
        uchar4 row[rsAllocationGetDimX(in)];
        for (int j = 0; j < rsAllocationGetDimX(in); j++){
            uchar4 pixel = rsGetElementAt_uchar4(in, i, j);
            row[j] = pixel;
        }
        selectionSort(row, rsAllocationGetDimX(in));
     }
  return in;
}

void process(rs_allocation inputImage, rs_allocation outputImage) {
   outputImage = invert(inputImage);
}

我简单地在asynctask中调用它,但是由于缺少调试rs的知识,位图为空,或者我不知道。

script.invoke_process(mInAllocation, outputAllocation);
outputAllocation.copyTo(bo);
android performance renderscript android-renderscript
1个回答
0
投票

您正在复制图像的每一行,然后对其进行排序,但是您永远不会回写结果(在任何地方都不会调用rsSetElement方法)。即使这样做,也不会认为使用该方法会获得令人满意的性能。我将通过编写一个可在您的输入分配的所有行上执行的内核(请查看Renderscirpt内核的LaunchOptions)来解决这个问题,因此它将至少在所有行上并行执行。看起来像:

rs_allocation allocIn;
rs_allocation allocOut;

void RS_KERNEL sortRows(uchar4 in, int x, int y){
    //with proper launch options, x stays always the same, while this kernel gets called in parallel for all rows of the input allocation ( = different y values)
    for (int currentCollumn = 0; currentCollumn  < rsAllocationGetDimX(allocIn); currentCollumn ++){
       //do your stuff for this row (by using y as index). Use rsGetElementAt and rsSetElementAt calls only (avoid copies for speed)   
    }
 }
© www.soinside.com 2019 - 2024. All rights reserved.