PixelSorting with Renderscript - PullRequest
       5

PixelSorting with Renderscript

1 голос
/ 10 апреля 2020

Я сделал небольшое приложение для сортировки пикселей с чистым 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);
}

Я просто вызываю его в асинхронной задаче, но растровое изображение пустое, или я не знаю, из-за отсутствия знаний об отладке rs.

script.invoke_process(mInAllocation, outputAllocation);
outputAllocation.copyTo(bo);

1 Ответ

2 голосов
/ 10 апреля 2020

Вы делаете копию каждой строки вашего изображения и затем сортируете ее, но никогда не записываете результат назад (нигде не вызывается метод rsSetElement). Даже когда вы делаете это, вы не думаете, что у вас будет удовлетворительная производительность с таким подходом. Я хотел бы подойти к этому, написав ядро, которое будет выполняться во всех строках вашего входного распределения (см. LaunchOptions ядер Renderscirpt), так что оно будет выполняться параллельно, по крайней мере, по всем строкам. Это будет выглядеть так:

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)   
    }
 }
Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...