Search code examples
androidcrenderscriptandroid-renderscript

Inconsistent behavior of Renderscript


I've written following renderscript:

ushort* curve_hth;
ushort* curve_hts;
ushort* curve_htv;
ushort* curve_sth;
ushort* curve_sts;
ushort* curve_stv;
ushort* curve_vth;
ushort* curve_vts;
ushort* curve_vtv;

uchar4 RS_KERNEL transform(uchar4 in, uint32_t x, uint32_t y)
{
    if(!isSelected(x, y)) return in;

    ushort3 in_rgb = { (ushort) in.r, (ushort) in.g, (ushort) in.b };
    ushort3 in_hsv = rgb_to_hsv(in_rgb);
    ushort in_h = in_hsv.r;
    ushort in_s = in_hsv.g;
    ushort in_v = in_hsv.b;
    ushort out_h, out_s, out_v;
    rsDebug("out1", out_s);
    if(curve_hth != NULL) out_h += curve_hth[in_h]; else out_h += in_h;
    rsDebug("out2", out_s);
    if(curve_hts != NULL) out_s += curve_hts[in_h];
    rsDebug("out3", out_s);
    if(curve_htv != NULL) out_v += curve_htv[in_h];
    rsDebug("out4", out_s);
    if(curve_sth != NULL) out_h += curve_sth[in_s];
    rsDebug("out5", out_s);
    if(curve_sts != NULL) out_s += curve_sts[in_s]; else out_s += in_s;
    rsDebug("out6", out_s);
    if(curve_stv != NULL) out_v += curve_stv[in_s];
    rsDebug("out7", out_s);
    if(curve_vth != NULL) out_h += curve_vth[in_v];
    rsDebug("out8", out_s);
    if(curve_vts != NULL) out_s += curve_vts[in_v];
    rsDebug("out9", out_s);
    if(curve_vtv != NULL) out_v += curve_vtv[in_v]; else out_v += in_v;
    rsDebug("out10", out_s);

    out_h = min((float) out_h, (float) 360);
    out_s = min((float) out_s, (float) 100);
    out_v = min((float) out_v, (float) 100);

    ushort3 out_hsv = { out_h, out_s, out_v };
    ushort3 out_rgb = hsv_to_rgb(out_hsv);
    uchar4 out = { out_rgb.r, out_rgb.g, out_rgb.b, in.a };
    return out;
}

When I run that script, I get in logcat:

D/RenderScript: out1 0  0x0
D/RenderScript: out2 0  0x0
D/RenderScript: out3 255  0xff
D/RenderScript: out4 255  0xff
D/RenderScript: out5 255  0xff
D/RenderScript: out6 255  0xff
D/RenderScript: out7 255  0xff
D/RenderScript: out8 255  0xff
D/RenderScript: out9 255  0xff
D/RenderScript: out10 255  0xff

It surprised me a lot, because all the curve_?t? pointers should be null, I don't bind them in Java code. It was strange, so I modified the debugging part of script and I got:

if(curve_hth != NULL) out_h += curve_hth[in_h]; else out_h += in_h;
rsDebug("out1", out_s);
rsDebug("out2", curve_hts == NULL);
if(curve_hts != NULL)
{
    out_s += curve_hts[in_h];
    rsDebug("out3", 1);
}
rsDebug("out4", out_s);
if(curve_htv != NULL) out_v += curve_htv[in_h];
if(curve_sth != NULL) out_h += curve_sth[in_s];
if(curve_sts != NULL) out_s += curve_sts[in_s]; else out_s += in_s;
if(curve_stv != NULL) out_v += curve_stv[in_s];
if(curve_vth != NULL) out_h += curve_vth[in_v];
if(curve_vts != NULL) out_s += curve_vts[in_v];
if(curve_vtv != NULL) out_v += curve_vtv[in_v]; else out_v += in_v;
rsDebug("out5", out_s);

The result in logcat was:

D/RenderScript: out1 0  0x0
D/RenderScript: out2 1  0x1
D/RenderScript: out4 25004  0x61ac
D/RenderScript: out5 25004  0x61ac

out2 is 1, so curve_hts is NULL. There is no out3 log, the if instruction doesn't get called, so how out_s changes its value?


Solution

  • Local variable out_h, out_s, out_v are not initialized, so the behavior is undefined.

    ushort out_h, out_s, out_v;
    

    If you initialize them to be 0, I think the problem should be gone.