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 android.util.Log;
     21 
     22 public class IntrinsicBlur extends IntrinsicBase {
     23     private ScriptIntrinsicBlur mIntrinsic;
     24     private int MAX_RADIUS = 25;
     25     private ScriptC_intrinsic_blur mScript;
     26     private float mRadius = MAX_RADIUS;
     27     private float mSaturation = 1.0f;
     28     private Allocation mScratchPixelsAllocation1;
     29     private Allocation mScratchPixelsAllocation2;
     30 
     31     @Override
     32     protected void tearDown() throws Exception {
     33         if (mIntrinsic != null) {
     34             mIntrinsic.destroy();
     35         }
     36         if (mScript != null) {
     37             mScript.destroy();
     38         }
     39         if (mScratchPixelsAllocation1 != null) {
     40             mScratchPixelsAllocation1.destroy();
     41         }
     42         if (mScratchPixelsAllocation2 != null) {
     43             mScratchPixelsAllocation2.destroy();
     44         }
     45         super.tearDown();
     46     }
     47 
     48     private void initTest(int w, int h, Element e, Script.LaunchOptions lo) {
     49         makeBuffers(w, h, e);
     50 
     51         Type.Builder tb = new Type.Builder(mRS, Element.F32_4(mRS));
     52         tb.setX(w);
     53         tb.setY(h);
     54         mScratchPixelsAllocation1 = Allocation.createTyped(mRS, tb.create());
     55         mScratchPixelsAllocation2 = Allocation.createTyped(mRS, tb.create());
     56 
     57         mIntrinsic = ScriptIntrinsicBlur.create(mRS, e);
     58         mIntrinsic.setRadius(MAX_RADIUS);
     59         mIntrinsic.setInput(mAllocSrc);
     60 
     61         mScript = new ScriptC_intrinsic_blur(mRS);
     62         mScript.set_width(w);
     63         mScript.set_height(h);
     64         mScript.invoke_setRadius(MAX_RADIUS);
     65 
     66         mScript.set_ScratchPixel1(mScratchPixelsAllocation1);
     67         mScript.set_ScratchPixel2(mScratchPixelsAllocation2);
     68 
     69         // Make reference
     70         copyInput();
     71         mScript.forEach_horz(mScratchPixelsAllocation2);
     72         mScript.forEach_vert(mScratchPixelsAllocation1);
     73         copyOutput(lo);
     74     }
     75 
     76     private void copyInput() {
     77         if (mAllocSrc.getType().getElement().isCompatible(Element.U8(mRS))) {
     78             mScript.forEach_convert1_uToF(mAllocSrc, mScratchPixelsAllocation1);
     79             return;
     80         }
     81         if (mAllocSrc.getType().getElement().isCompatible(Element.U8_4(mRS))) {
     82             mScript.forEach_convert4_uToF(mAllocSrc, mScratchPixelsAllocation1);
     83             return;
     84         }
     85         throw new IllegalArgumentException("bad type");
     86     }
     87 
     88     private void copyOutput(Script.LaunchOptions lo) {
     89         if (mAllocSrc.getType().getElement().isCompatible(Element.U8(mRS))) {
     90             mScript.forEach_convert1_fToU(mScratchPixelsAllocation1, mAllocRef, lo);
     91             return;
     92         }
     93         if (mAllocSrc.getType().getElement().isCompatible(Element.U8_4(mRS))) {
     94             mScript.forEach_convert4_fToU(mScratchPixelsAllocation1, mAllocRef, lo);
     95             return;
     96         }
     97         throw new IllegalArgumentException("bad type");
     98     }
     99 
    100     public void testU8_1() {
    101         final int w = 97;
    102         final int h = 97;
    103         Element e = Element.U8(mRS);
    104         initTest(w, h, e, null);
    105 
    106         mIntrinsic.forEach(mAllocDst);
    107 
    108         mVerify.set_gAllowedIntError(1);
    109         mVerify.invoke_verify(mAllocRef, mAllocDst, mAllocSrc);
    110         mRS.finish();
    111         checkError();
    112     }
    113 
    114     public void testU8_4() {
    115         final int w = 97;
    116         final int h = 97;
    117         Element e = Element.U8_4(mRS);
    118         initTest(w, h, e, null);
    119 
    120         mIntrinsic.forEach(mAllocDst);
    121 
    122         mVerify.set_gAllowedIntError(1);
    123         mVerify.invoke_verify(mAllocRef, mAllocDst, mAllocSrc);
    124         mRS.finish();
    125         checkError();
    126     }
    127 
    128 
    129     public void testU8_1C() {
    130         final int w = 97;
    131         final int h = 97;
    132         Element e = Element.U8(mRS);
    133         Script.LaunchOptions lo = makeClipper(11, 11, w - 11, h - 11);
    134 
    135         initTest(w, h, e, lo);
    136         mIntrinsic.forEach(mAllocDst, lo);
    137 
    138         mVerify.set_gAllowedIntError(1);
    139         mVerify.invoke_verify(mAllocRef, mAllocDst, mAllocSrc);
    140         mRS.finish();
    141         checkError();
    142     }
    143 
    144     public void testU8_4C() {
    145         final int w = 97;
    146         final int h = 97;
    147         Element e = Element.U8_4(mRS);
    148         Script.LaunchOptions lo = makeClipper(11, 11, w - 11, h - 11);
    149 
    150         initTest(w, h, e, lo);
    151         mIntrinsic.forEach(mAllocDst, lo);
    152 
    153         mVerify.set_gAllowedIntError(1);
    154         mVerify.invoke_verify(mAllocRef, mAllocDst, mAllocSrc);
    155         mRS.finish();
    156         checkError();
    157     }
    158 
    159     public void test_ID() {
    160         ScriptIntrinsicBlur s = ScriptIntrinsicBlur.create(mRS, Element.U8_4(mRS));
    161         Script.KernelID kid = s.getKernelID();
    162         if (kid == null) {
    163             throw new IllegalStateException("kid must be valid");
    164         }
    165 
    166         Script.FieldID fid = s.getFieldID_Input();
    167         if (fid == null) {
    168             throw new IllegalStateException("fid must be valid");
    169         }
    170 
    171         s.destroy();
    172     }
    173 
    174 }
    175