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.cts.rscpp;
18 
19 import android.content.Context;
20 import android.content.res.Resources;
21 import android.test.AndroidTestCase;
22 import android.renderscript.RenderScript.RSErrorHandler;
23 import android.renderscript.RenderScript.RSMessageHandler;
24 import android.renderscript.RSRuntimeException;
25 import android.renderscript.RenderScript;
26 import android.renderscript.Allocation;
27 import android.renderscript.Element;
28 import android.renderscript.Type;
29 import android.util.Log;
30 
31 public class RSCppTest extends AndroidTestCase {
32 
33     Context mCtx;
34     Resources mRes;
35     RenderScript mRS;
36     protected ScriptC_verify mVerify;
37 
38     private int result;
39     private boolean msgHandled;
40 
41     private static final int RS_MSG_TEST_PASSED = 100;
42     private static final int RS_MSG_TEST_FAILED = 101;
43 
44     RSMessageHandler mRsMessage = new RSMessageHandler() {
45         public void run() {
46             if (result == 0) {
47                 switch (mID) {
48                     case RS_MSG_TEST_PASSED:
49                     case RS_MSG_TEST_FAILED:
50                         result = mID;
51                         break;
52                     default:
53                         fail("Got unexpected RS message");
54                         return;
55                 }
56             }
57             msgHandled = true;
58         }
59     };
60 
waitForMessage()61     protected void waitForMessage() {
62         while (!msgHandled) {
63             Thread.yield();
64         }
65     }
66 
67     protected boolean FoundError = false;
68     protected RSErrorHandler mRsError = new RSErrorHandler() {
69         public void run() {
70             FoundError = true;
71             Log.e("RSCppCTS", mErrorMessage);
72             throw new RSRuntimeException("Received error " + mErrorNum +
73                                          " message " + mErrorMessage);
74         }
75     };
76 
makeElement(Element.DataType dt, int vecSize)77     protected Element makeElement(Element.DataType dt, int vecSize) {
78         Element e;
79         if (vecSize > 1) {
80             e = Element.createVector(mRS, dt, vecSize);
81         } else {
82             if (dt == Element.DataType.UNSIGNED_8) {
83                 e = Element.U8(mRS);
84             } else {
85                 e = Element.F32(mRS);
86             }
87         }
88         return e;
89     }
90 
makeAllocation(int w, int h, Element e)91     protected Allocation makeAllocation(int w, int h, Element e) {
92         Type.Builder tb = new Type.Builder(mRS, e);
93         tb.setX(w);
94         tb.setY(h);
95         Type t = tb.create();
96         Allocation a = Allocation.createTyped(mRS, t);
97         return a;
98     }
99 
checkForErrors()100     protected void checkForErrors() {
101         mRS.finish();
102         mVerify.invoke_checkError();
103         waitForMessage();
104         assertFalse(FoundError);
105         assertTrue(result != RS_MSG_TEST_FAILED);
106     }
107 
108     @Override
setUp()109     protected void setUp() throws Exception {
110         super.setUp();
111         result = 0;
112         msgHandled = false;
113         mCtx = getContext();
114         mRes = mCtx.getResources();
115         mRS = RenderScript.create(mCtx);
116         mRS.setMessageHandler(mRsMessage);
117         mVerify = new ScriptC_verify(mRS);
118         mVerify.set_gAllowedIntError(3);
119     }
120 
121     @Override
tearDown()122     protected void tearDown() throws Exception {
123         if (mVerify != null) {
124             mVerify.destroy();
125             mVerify = null;
126         }
127         if (mRS != null) {
128             mRS.destroy();
129             mRS = null;
130         }
131         super.tearDown();
132     }
133 }
134