Home | History | Annotate | Download | only in cts
      1 /*
      2  * Copyright (C) 2013 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.renderscript.cts;
     18 
     19 import android.renderscript.*;
     20 import java.util.Random;
     21 
     22 public class YuvTest extends RSBaseCompute {
     23     int width;
     24     int height;
     25     byte [] by;
     26     byte [] bu;
     27     byte [] bv;
     28     Allocation ay;
     29     Allocation au;
     30     Allocation av;
     31 
     32     protected ScriptC_verify mVerify;
     33 
     34     @Override
     35     protected void tearDown() throws Exception {
     36         if (ay != null) {
     37             ay.destroy();
     38         }
     39         if (au != null) {
     40             au.destroy();
     41         }
     42         if (av != null) {
     43             av.destroy();
     44         }
     45         if (mVerify != null) {
     46             mVerify.destroy();
     47         }
     48     }
     49 
     50     int getCWidth() {
     51         return (width + 1) / 2;
     52     }
     53     int getCHeight() {
     54         return (height + 1) / 2;
     55     }
     56 
     57     protected void makeYuvBuffer(int w, int h) {
     58         Random r = new Random();
     59         width = w;
     60         height = h;
     61 
     62         by = new byte[w*h];
     63         bu = new byte[getCWidth() * getCHeight()];
     64         bv = new byte[getCWidth() * getCHeight()];
     65 
     66         for (int i=0; i < by.length; i++) {
     67             by[i] = (byte)r.nextInt(256);
     68         }
     69         for (int i=0; i < bu.length; i++) {
     70             bu[i] = (byte)r.nextInt(256);
     71         }
     72         for (int i=0; i < bv.length; i++) {
     73             bv[i] = (byte)r.nextInt(256);
     74         }
     75 
     76         ay = Allocation.createTyped(mRS, Type.createXY(mRS, Element.U8(mRS), w, h));
     77         final Type tuv = Type.createXY(mRS, Element.U8(mRS), w >> 1, h >> 1);
     78         au = Allocation.createTyped(mRS, tuv);
     79         av = Allocation.createTyped(mRS, tuv);
     80 
     81         ay.copyFrom(by);
     82         au.copyFrom(bu);
     83         av.copyFrom(bv);
     84     }
     85 
     86     public Allocation makeOutput() {
     87         return Allocation.createTyped(mRS, Type.createXY(mRS, Element.RGBA_8888(mRS), width, height));
     88     }
     89 
     90     public Allocation makeOutput_f4() {
     91         return Allocation.createTyped(mRS, Type.createXY(mRS, Element.F32_4(mRS), width, height));
     92     }
     93     // Test for the API 17 conversion path
     94     // This used a uchar buffer assuming nv21
     95     public void testV17() {
     96         mVerify = new ScriptC_verify(mRS);
     97 
     98         makeYuvBuffer(120, 96);
     99         Allocation aout = makeOutput();
    100         Allocation aref = makeOutput();
    101 
    102         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
    103         int i = 0;
    104         for (int j = 0; j < (width * height); j++) {
    105             tmp[i++] = by[j];
    106         }
    107         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    108             tmp[i++] = bv[j];
    109             tmp[i++] = bu[j];
    110         }
    111 
    112         Allocation ta = Allocation.createSized(mRS, Element.U8(mRS), tmp.length);
    113         ta.copyFrom(tmp);
    114 
    115         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.U8(mRS));
    116         syuv.setInput(ta);
    117         syuv.forEach(aout);
    118 
    119         mVerify.set_gAllowedIntError(2); // this will allow for less strict implementation
    120         ScriptC_yuv script = new ScriptC_yuv(mRS);
    121         script.invoke_makeRef(ay, au, av, aref);
    122 
    123         mVerify.invoke_verify(aref, aout, ay);
    124 
    125         mRS.finish();
    126         mVerify.invoke_checkError();
    127         waitForMessage();
    128 
    129         ta.destroy();
    130         syuv.destroy();
    131         script.destroy();
    132 
    133         checkForErrors();
    134     }
    135 
    136     // Test for the API 18 conversion path with nv21
    137     public void test_NV21() {
    138         mVerify = new ScriptC_verify(mRS);
    139         ScriptC_yuv script = new ScriptC_yuv(mRS);
    140         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.YUV(mRS));
    141 
    142         makeYuvBuffer(512, 512);
    143         Allocation aout = makeOutput();
    144         Allocation aref = makeOutput();
    145 
    146 
    147         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
    148         tb.setX(width);
    149         tb.setY(height);
    150         tb.setYuvFormat(android.graphics.ImageFormat.NV21);
    151         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    152 
    153         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
    154         int i = 0;
    155         for (int j = 0; j < (width * height); j++) {
    156             tmp[i++] = by[j];
    157         }
    158         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    159             tmp[i++] = bv[j];
    160             tmp[i++] = bu[j];
    161         }
    162         ta.copyFrom(tmp);
    163         script.invoke_makeRef(ay, au, av, aref);
    164 
    165         mVerify.set_gAllowedIntError(2); // this will allow for less strict implementation
    166         syuv.setInput(ta);
    167         syuv.forEach(aout);
    168         mVerify.invoke_verify(aref, aout, ay);
    169 
    170         script.set_mInput(ta);
    171         script.forEach_cvt(aout);
    172         mVerify.invoke_verify(aref, aout, ay);
    173 
    174         mRS.finish();
    175         mVerify.invoke_checkError();
    176         waitForMessage();
    177 
    178         aout.destroy();
    179         aref.destroy();
    180         ta.destroy();
    181         script.destroy();
    182         syuv.destroy();
    183 
    184         checkForErrors();
    185     }
    186 
    187     // Test for the API 18 conversion path with yv12
    188     public void test_YV12() {
    189         mVerify = new ScriptC_verify(mRS);
    190         ScriptC_yuv script = new ScriptC_yuv(mRS);
    191         ScriptIntrinsicYuvToRGB syuv = ScriptIntrinsicYuvToRGB.create(mRS, Element.YUV(mRS));
    192 
    193         makeYuvBuffer(512, 512);
    194         Allocation aout = makeOutput();
    195         Allocation aref = makeOutput();
    196 
    197 
    198         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
    199         tb.setX(width);
    200         tb.setY(height);
    201         tb.setYuvFormat(android.graphics.ImageFormat.YV12);
    202         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    203 
    204         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
    205         int i = 0;
    206         for (int j = 0; j < (width * height); j++) {
    207             tmp[i++] = by[j];
    208         }
    209         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    210             tmp[i++] = bu[j];
    211         }
    212         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    213             tmp[i++] = bv[j];
    214         }
    215         ta.copyFrom(tmp);
    216         script.invoke_makeRef(ay, au, av, aref);
    217         mVerify.set_gAllowedIntError(2); // this will allow for less strict implementation
    218 
    219         syuv.setInput(ta);
    220         syuv.forEach(aout);
    221         mVerify.invoke_verify(aref, aout, ay);
    222 
    223         script.set_mInput(ta);
    224         script.forEach_cvt(aout);
    225         mVerify.invoke_verify(aref, aout, ay);
    226 
    227         mRS.finish();
    228         mVerify.invoke_checkError();
    229         waitForMessage();
    230 
    231         aout.destroy();
    232         aref.destroy();
    233         ta.destroy();
    234         script.destroy();
    235         syuv.destroy();
    236 
    237         checkForErrors();
    238     }
    239 
    240     // Test for the API conversion to float4 RGBA using rsYuvToRGBA, YV12.
    241     public void test_YV12_Float4() {
    242         mVerify = new ScriptC_verify(mRS);
    243         ScriptC_yuv script = new ScriptC_yuv(mRS);
    244 
    245         makeYuvBuffer(512, 512);
    246         Allocation aout = makeOutput_f4();
    247         Allocation aref = makeOutput_f4();
    248 
    249 
    250         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
    251         tb.setX(width);
    252         tb.setY(height);
    253         tb.setYuvFormat(android.graphics.ImageFormat.YV12);
    254         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    255 
    256         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
    257         int i = 0;
    258         for (int j = 0; j < (width * height); j++) {
    259             tmp[i++] = by[j];
    260         }
    261         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    262             tmp[i++] = bu[j];
    263         }
    264         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    265             tmp[i++] = bv[j];
    266         }
    267         ta.copyFrom(tmp);
    268         script.invoke_makeRef_f4(ay, au, av, aref);
    269 
    270         script.set_mInput(ta);
    271         script.forEach_cvt_f4(aout);
    272         mVerify.invoke_verify(aref, aout, ay);
    273 
    274         mRS.finish();
    275         mVerify.set_gAllowedFloatError(0.01f); // this will allow for less strict implementation
    276         mVerify.invoke_checkError();
    277         waitForMessage();
    278 
    279         aout.destroy();
    280         aref.destroy();
    281         ta.destroy();
    282         script.destroy();
    283 
    284         checkForErrors();
    285     }
    286 
    287     // Test for the API conversion to float4 RGBA using rsYuvToRGBA, NV21.
    288     public void test_NV21_Float4() {
    289         mVerify = new ScriptC_verify(mRS);
    290         ScriptC_yuv script = new ScriptC_yuv(mRS);
    291 
    292         makeYuvBuffer(512, 512);
    293         Allocation aout = makeOutput_f4();
    294         Allocation aref = makeOutput_f4();
    295 
    296 
    297         Type.Builder tb = new Type.Builder(mRS, Element.YUV(mRS));
    298         tb.setX(width);
    299         tb.setY(height);
    300         tb.setYuvFormat(android.graphics.ImageFormat.NV21);
    301         Allocation ta = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_SCRIPT);
    302 
    303         byte tmp[] = new byte[(width * height) + (getCWidth() * getCHeight() * 2)];
    304         int i = 0;
    305         for (int j = 0; j < (width * height); j++) {
    306             tmp[i++] = by[j];
    307         }
    308         for (int j = 0; j < (getCWidth() * getCHeight()); j++) {
    309             tmp[i++] = bv[j];
    310             tmp[i++] = bu[j];
    311         }
    312 
    313         ta.copyFrom(tmp);
    314         script.invoke_makeRef_f4(ay, au, av, aref);
    315 
    316         script.set_mInput(ta);
    317         script.forEach_cvt_f4(aout);
    318         mVerify.set_gAllowedFloatError(0.01f); // this will allow for less strict implementation
    319         mVerify.invoke_verify(aref, aout, ay);
    320 
    321         mRS.finish();
    322         mVerify.invoke_checkError();
    323         waitForMessage();
    324 
    325         aout.destroy();
    326         aref.destroy();
    327         ta.destroy();
    328         script.destroy();
    329 
    330         checkForErrors();
    331     }
    332 }
    333