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 #include "shared.rsh"
     18 
     19 int gAllowedIntError = 0;
     20 float gAllowedFloatError = 0.0001f;
     21 static bool hadError = false;
     22 static int2 errorLoc = {0,0};
     23 
     24 static bool compare_float(float f1, float f2) {
     25     if (fabs(f1-f2) > gAllowedFloatError) {
     26         hadError = true;
     27         return false;
     28     }
     29     return true;
     30 }
     31 
     32 static bool verify_float4(rs_allocation in1, rs_allocation in2) {
     33     uint32_t w = rsAllocationGetDimX(in1);
     34     uint32_t h = rsAllocationGetDimY(in1);
     35     for (uint32_t y = 0; y < h; y++) {
     36         for (uint32_t x=0; x < w; x++) {
     37             float4 pref = rsGetElementAt_float4(in1, x, y);
     38             float4 ptst = rsGetElementAt_float4(in2, x, y);
     39             bool e = !compare_float(pref.x, ptst.x);
     40             e |= !compare_float(pref.y, ptst.y);
     41             e |= !compare_float(pref.z, ptst.z);
     42             e |= !compare_float(pref.w, ptst.w);
     43             if (e) {
     44                 errorLoc.x = x;
     45                 errorLoc.y = y;
     46                 return false;
     47             }
     48         }
     49     }
     50     return true;
     51 }
     52 
     53 static bool verify_float3(rs_allocation in1, rs_allocation in2) {
     54     uint32_t w = rsAllocationGetDimX(in1);
     55     uint32_t h = rsAllocationGetDimY(in1);
     56     for (uint32_t y = 0; y < h; y++) {
     57         for (uint32_t x=0; x < w; x++) {
     58             float3 pref = rsGetElementAt_float3(in1, x, y);
     59             float3 ptst = rsGetElementAt_float3(in2, x, y);
     60             bool e = !compare_float(pref.x, ptst.x);
     61             e |= !compare_float(pref.y, ptst.y);
     62             e |= !compare_float(pref.z, ptst.z);
     63             if (e) {
     64                 errorLoc.x = x;
     65                 errorLoc.y = y;
     66                 return false;
     67             }
     68         }
     69     }
     70     return true;
     71 }
     72 
     73 static bool verify_float2(rs_allocation in1, rs_allocation in2) {
     74     uint32_t w = rsAllocationGetDimX(in1);
     75     uint32_t h = rsAllocationGetDimY(in1);
     76     for (uint32_t y = 0; y < h; y++) {
     77         for (uint32_t x=0; x < w; x++) {
     78             float2 pref = rsGetElementAt_float2(in1, x, y);
     79             float2 ptst = rsGetElementAt_float2(in2, x, y);
     80             bool e = !compare_float(pref.x, ptst.x);
     81             e |= !compare_float(pref.y, ptst.y);
     82             if (e) {
     83                 errorLoc.x = x;
     84                 errorLoc.y = y;
     85                 return false;
     86             }
     87         }
     88     }
     89     return true;
     90 }
     91 
     92 static bool verify_float(rs_allocation in1, rs_allocation in2) {
     93     uint32_t w = rsAllocationGetDimX(in1);
     94     uint32_t h = rsAllocationGetDimY(in1);
     95     for (uint32_t y = 0; y < h; y++) {
     96         for (uint32_t x=0; x < w; x++) {
     97             float pref = rsGetElementAt_float(in1, x, y);
     98             float ptst = rsGetElementAt_float(in2, x, y);
     99             bool e = !compare_float(pref, ptst);
    100             if (e) {
    101                 errorLoc.x = x;
    102                 errorLoc.y = y;
    103                 return false;
    104             }
    105         }
    106     }
    107     return true;
    108 }
    109 
    110 static bool verify_uchar4(rs_allocation in1, rs_allocation in2) {
    111     int merr = 0;
    112     uint32_t w = rsAllocationGetDimX(in1);
    113     uint32_t h = rsAllocationGetDimY(in1);
    114     for (uint32_t y = 0; y < h; y++) {
    115         for (uint32_t x=0; x < w; x++) {
    116             int4 pref = convert_int4(rsGetElementAt_uchar4(in1, x, y));
    117             int4 ptst = convert_int4(rsGetElementAt_uchar4(in2, x, y));
    118             int4 d = convert_int4(abs(pref - ptst));
    119             int e = 0;
    120             e = max(e, d.x);
    121             e = max(e, d.y);
    122             e = max(e, d.z);
    123             e = max(e, d.w);
    124             if (e > gAllowedIntError) {
    125                 errorLoc.x = x;
    126                 errorLoc.y = y;
    127                 hadError = true;
    128                 return false;
    129             }
    130             merr = max(e, merr);
    131         }
    132     }
    133     return true;
    134 }
    135 
    136 static bool verify_uchar3(rs_allocation in1, rs_allocation in2) {
    137     int merr = 0;
    138     uint32_t w = rsAllocationGetDimX(in1);
    139     uint32_t h = rsAllocationGetDimY(in1);
    140     for (uint32_t y = 0; y < h; y++) {
    141         for (uint32_t x=0; x < w; x++) {
    142             int3 pref = convert_int3(rsGetElementAt_uchar3(in1, x, y));
    143             int3 ptst = convert_int3(rsGetElementAt_uchar3(in2, x, y));
    144             int3 d = convert_int3(abs(pref - ptst));
    145             int e = 0;
    146             e = max(e, d.x);
    147             e = max(e, d.y);
    148             e = max(e, d.z);
    149             if (e > gAllowedIntError) {
    150                 errorLoc.x = x;
    151                 errorLoc.y = y;
    152                 hadError = true;
    153                 return false;
    154             }
    155             merr = max(e, merr);
    156         }
    157     }
    158     return true;
    159 }
    160 
    161 static bool verify_uchar2(rs_allocation in1, rs_allocation in2) {
    162     int merr = 0;
    163     uint32_t w = rsAllocationGetDimX(in1);
    164     uint32_t h = rsAllocationGetDimY(in1);
    165     for (uint32_t y = 0; y < h; y++) {
    166         for (uint32_t x=0; x < w; x++) {
    167             int2 pref = convert_int2(rsGetElementAt_uchar2(in1, x, y));
    168             int2 ptst = convert_int2(rsGetElementAt_uchar2(in2, x, y));
    169             int2 d = convert_int2(abs(pref - ptst));
    170             int e = 0;
    171             e = max(e, d.x);
    172             e = max(e, d.y);
    173             if (e > gAllowedIntError) {
    174                 errorLoc.x = x;
    175                 errorLoc.y = y;
    176                 hadError = true;
    177                 return false;
    178             }
    179             merr = max(e, merr);
    180         }
    181     }
    182     return true;
    183 }
    184 
    185 static bool verify_uchar(rs_allocation in1, rs_allocation in2) {
    186     int merr = 0;
    187     uint32_t w = rsAllocationGetDimX(in1);
    188     uint32_t h = rsAllocationGetDimY(in1);
    189     for (uint32_t y = 0; y < h; y++) {
    190         for (uint32_t x=0; x < w; x++) {
    191             int pref = rsGetElementAt_uchar(in1, x, y);
    192             int ptst = rsGetElementAt_uchar(in2, x, y);
    193             int e = abs(pref - ptst);
    194             if (e > gAllowedIntError) {
    195                 errorLoc.x = x;
    196                 errorLoc.y = y;
    197                 hadError = true;
    198                 return false;
    199             }
    200             merr = max(e, merr);
    201         }
    202     }
    203     return true;
    204 }
    205 
    206 #define printCell(txt, a, xy) \
    207 {                       \
    208     rs_element e = rsAllocationGetElement(a); \
    209     rs_data_type dt = rsElementGetDataType(e); \
    210     uint32_t vs = rsElementGetVectorSize(e); \
    211  \
    212     if (dt == RS_TYPE_UNSIGNED_8) { \
    213         switch(vs) { \
    214         case 4: \
    215             rsDebug(txt, rsGetElementAt_uchar4(a, xy.x, xy.y)); \
    216             break; \
    217         case 3: \
    218             rsDebug(txt, rsGetElementAt_uchar3(a, xy.x, xy.y)); \
    219             break; \
    220         case 2: \
    221             rsDebug(txt, rsGetElementAt_uchar2(a, xy.x, xy.y)); \
    222             break; \
    223         case 1: \
    224             rsDebug(txt, rsGetElementAt_uchar(a, xy.x, xy.y)); \
    225             break; \
    226         } \
    227     } else if (dt == RS_TYPE_FLOAT_32) { \
    228         switch(vs) { \
    229         case 4: \
    230             rsDebug(txt, rsGetElementAt_float4(a, xy.x, xy.y)); \
    231             break; \
    232         case 3: \
    233             rsDebug(txt, rsGetElementAt_float3(a, xy.x, xy.y)); \
    234             break; \
    235         case 2: \
    236             rsDebug(txt, rsGetElementAt_float2(a, xy.x, xy.y)); \
    237             break; \
    238         case 1: \
    239             rsDebug(txt, rsGetElementAt_float(a, xy.x, xy.y)); \
    240             break; \
    241         } \
    242     } else if (dt == RS_TYPE_FLOAT_64) { \
    243         switch(vs) { \
    244         case 4: \
    245             rsDebug(txt, rsGetElementAt_double4(a, xy.x, xy.y)); \
    246             break; \
    247         case 3: \
    248             rsDebug(txt, rsGetElementAt_double3(a, xy.x, xy.y)); \
    249             break; \
    250         case 2: \
    251             rsDebug(txt, rsGetElementAt_double2(a, xy.x, xy.y)); \
    252             break; \
    253         case 1: \
    254             rsDebug(txt, rsGetElementAt_double(a, xy.x, xy.y)); \
    255         } \
    256     } \
    257 }
    258 
    259 void verify(rs_allocation ref_in, rs_allocation tst_in, rs_allocation src_in) {
    260     rs_element e = rsAllocationGetElement(ref_in);
    261     rs_data_type dt = rsElementGetDataType(e);
    262     uint32_t vs = rsElementGetVectorSize(e);
    263     bool valid = false;
    264 
    265     if (dt == RS_TYPE_UNSIGNED_8) {
    266         switch(vs) {
    267         case 4:
    268             valid = verify_uchar4(ref_in, tst_in);
    269             break;
    270         case 3:
    271             valid = verify_uchar3(ref_in, tst_in);
    272             break;
    273         case 2:
    274             valid = verify_uchar2(ref_in, tst_in);
    275             break;
    276         case 1:
    277             valid = verify_uchar(ref_in, tst_in);
    278             break;
    279         }
    280     } else {
    281         switch(vs) {
    282         case 4:
    283             valid = verify_float4(ref_in, tst_in);
    284             break;
    285         case 3:
    286             valid = verify_float3(ref_in, tst_in);
    287             break;
    288         case 2:
    289             valid = verify_float2(ref_in, tst_in);
    290             break;
    291         case 1:
    292             valid = verify_float(ref_in, tst_in);
    293             break;
    294         }
    295     }
    296     if (!valid) {
    297         rsDebug("verify failure at xy", errorLoc);
    298         printCell("start value     ", src_in, errorLoc);
    299         printCell("reference value ", ref_in, errorLoc);
    300         printCell("test value      ", tst_in, errorLoc);
    301     }
    302 }
    303 
    304 void checkError()
    305 {
    306     if (hadError) {
    307         rsSendToClientBlocking(RS_MSG_TEST_FAILED);
    308     } else {
    309         rsSendToClientBlocking(RS_MSG_TEST_PASSED);
    310     }
    311 }
    312