1/*
2 * Copyright (C) 2017 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
5 * in compliance with the License. You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software distributed under the License
10 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
11 * or implied. See the License for the specific language governing permissions and limitations under
12 * the License.
13 */
14
15package com.android.rs.test;
16
17import android.content.Context;
18import android.renderscript.Allocation;
19import android.renderscript.Element;
20import android.renderscript.RenderScript;
21import android.renderscript.RSIllegalArgumentException;
22import android.renderscript.ScriptIntrinsicBlur;
23import android.renderscript.Type;
24import android.util.Log;
25
26// Tests that ScriptIntrinsicBlur properly throws exception if input or output
27// are set to 1D Allocations.
28public class UT_blur_validation extends UnitTest {
29    private static final String TAG = "ScriptIntrinsicBlur validation";
30    private RenderScript RS;
31    private Allocation input1D, output1D;
32    private Allocation input2D, output2D;
33    private ScriptIntrinsicBlur scriptBlur;
34
35    protected UT_blur_validation(RSTestCore rstc, Context ctx) {
36        super(rstc, TAG, ctx);
37    }
38
39    private void cleanup() {
40        RS.finish();
41        input1D.destroy();
42        input2D.destroy();
43        output1D.destroy();
44        output2D.destroy();
45        scriptBlur.destroy();
46        RS.destroy();
47    }
48
49    public void run() {
50        RS = RenderScript.create(mCtx);
51
52        final int width  = 100;
53        final int height = 100;
54
55        input1D = Allocation.createSized(RS,
56                                         Element.U8(RS),
57                                         width * height,
58                                         Allocation.USAGE_SCRIPT);
59
60        output1D = Allocation.createTyped(RS, input1D.getType());
61
62        Type.Builder typeBuilder = new Type.Builder(RS, Element.U8(RS));
63        typeBuilder.setX(width);
64        typeBuilder.setY(height);
65        Type ty = typeBuilder.create();
66
67        input2D  = Allocation.createTyped(RS, ty);
68        output2D = Allocation.createTyped(RS, ty);
69
70        scriptBlur = ScriptIntrinsicBlur.create(RS, Element.U8(RS));
71
72        scriptBlur.setRadius(25f);
73        boolean failed = false;
74        try {
75            scriptBlur.setInput(input1D);
76        } catch (RSIllegalArgumentException e) {
77            scriptBlur.setInput(input2D);
78            try {
79                scriptBlur.forEach(output1D);
80            } catch (RSIllegalArgumentException e1) {
81                scriptBlur.forEach(output2D);
82                cleanup();
83                passTest();
84                return;
85            }
86            Log.e(TAG, "setting 1d output does not trigger exception");
87            cleanup();
88            failTest();
89            return;
90        }
91
92        Log.e(TAG, "setting 1d input does not trigger exception");
93        cleanup();
94        failTest();
95    }
96}
97