1/*
2 * Copyright (C) 2008-2012 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
17package com.android.rs.test;
18
19import android.content.Context;
20import android.content.res.Resources;
21import android.renderscript.*;
22import android.util.Log;
23import java.util.ArrayList;
24import java.util.ListIterator;
25import java.util.Timer;
26import java.util.TimerTask;
27
28
29public class RSTestCore {
30    int mWidth;
31    int mHeight;
32    Context mCtx;
33
34    public RSTestCore(Context ctx) {
35        mCtx = ctx;
36    }
37
38    private Resources mRes;
39    private RenderScriptGL mRS;
40
41    private Font mFont;
42    ScriptField_ListAllocs_s mListAllocs;
43    int mLastX;
44    int mLastY;
45    private ScriptC_rslist mScript;
46
47    private ArrayList<UnitTest> unitTests;
48    private ListIterator<UnitTest> test_iter;
49    private UnitTest activeTest;
50    private boolean stopTesting;
51
52    /* Periodic timer for ensuring future tests get scheduled */
53    private Timer mTimer;
54    public static final int RS_TIMER_PERIOD = 100;
55
56    public void init(RenderScriptGL rs, Resources res, int width, int height) {
57        mRS = rs;
58        mRes = res;
59        mWidth = width;
60        mHeight = height;
61        stopTesting = false;
62
63        mScript = new ScriptC_rslist(mRS, mRes, R.raw.rslist);
64
65        unitTests = new ArrayList<UnitTest>();
66
67        unitTests.add(new UT_primitives(this, mRes, mCtx));
68        unitTests.add(new UT_constant(this, mRes, mCtx));
69        unitTests.add(new UT_vector(this, mRes, mCtx));
70        unitTests.add(new UT_unsigned(this, mRes, mCtx));
71        unitTests.add(new UT_array_init(this, mRes, mCtx));
72        unitTests.add(new UT_array_alloc(this, mRes, mCtx));
73        unitTests.add(new UT_kernel(this, mRes, mCtx));
74        unitTests.add(new UT_kernel_struct(this, mRes, mCtx));
75        unitTests.add(new UT_bug_char(this, mRes, mCtx));
76        unitTests.add(new UT_clamp(this, mRes, mCtx));
77        unitTests.add(new UT_clamp_relaxed(this, mRes, mCtx));
78        unitTests.add(new UT_convert(this, mRes, mCtx));
79        unitTests.add(new UT_convert_relaxed(this, mRes, mCtx));
80        unitTests.add(new UT_copy_test(this, mRes, mCtx));
81        unitTests.add(new UT_rsdebug(this, mRes, mCtx));
82        unitTests.add(new UT_rstime(this, mRes, mCtx));
83        unitTests.add(new UT_rstypes(this, mRes, mCtx));
84        unitTests.add(new UT_alloc(this, mRes, mCtx));
85        unitTests.add(new UT_check_dims(this, mRes, mCtx));
86        unitTests.add(new UT_static_globals(this, mRes, mCtx));
87        unitTests.add(new UT_refcount(this, mRes, mCtx));
88        unitTests.add(new UT_foreach(this, mRes, mCtx));
89        unitTests.add(new UT_foreach_bounds(this, mRes, mCtx));
90        unitTests.add(new UT_noroot(this, mRes, mCtx));
91        unitTests.add(new UT_atomic(this, mRes, mCtx));
92        unitTests.add(new UT_struct(this, mRes, mCtx));
93        unitTests.add(new UT_math(this, mRes, mCtx));
94        unitTests.add(new UT_math_conformance(this, mRes, mCtx));
95        unitTests.add(new UT_math_agree(this, mRes, mCtx));
96        unitTests.add(new UT_min(this, mRes, mCtx));
97        unitTests.add(new UT_int4(this, mRes, mCtx));
98        unitTests.add(new UT_element(this, mRes, mCtx));
99        unitTests.add(new UT_sampler(this, mRes, mCtx));
100        unitTests.add(new UT_program_store(this, mRes, mCtx));
101        unitTests.add(new UT_program_raster(this, mRes, mCtx));
102        unitTests.add(new UT_mesh(this, mRes, mCtx));
103        unitTests.add(new UT_fp_mad(this, mRes, mCtx));
104
105        /*
106        unitTests.add(new UnitTest(null, "<Pass>", 1));
107        unitTests.add(new UnitTest());
108        unitTests.add(new UnitTest(null, "<Fail>", -1));
109
110        for (int i = 0; i < 20; i++) {
111            unitTests.add(new UnitTest(null, "<Pass>", 1));
112        }
113        */
114
115        UnitTest [] uta = new UnitTest[unitTests.size()];
116        uta = unitTests.toArray(uta);
117
118        mListAllocs = new ScriptField_ListAllocs_s(mRS, uta.length);
119        for (int i = 0; i < uta.length; i++) {
120            ScriptField_ListAllocs_s.Item listElem = new ScriptField_ListAllocs_s.Item();
121            listElem.text = Allocation.createFromString(mRS, uta[i].name, Allocation.USAGE_SCRIPT);
122            listElem.result = uta[i].getResult();
123            mListAllocs.set(listElem, i, false);
124            uta[i].setItem(listElem);
125        }
126
127        mListAllocs.copyAll();
128
129        mScript.bind_gList(mListAllocs);
130
131        mFont = Font.create(mRS, mRes, "serif", Font.Style.BOLD, 8);
132        mScript.set_gFont(mFont);
133
134        mRS.bindRootScript(mScript);
135
136        test_iter = unitTests.listIterator();
137        refreshTestResults(); /* Kick off the first test */
138
139        TimerTask pTask = new TimerTask() {
140            public void run() {
141                refreshTestResults();
142            }
143        };
144
145        mTimer = new Timer();
146        mTimer.schedule(pTask, RS_TIMER_PERIOD, RS_TIMER_PERIOD);
147    }
148
149    public void checkAndRunNextTest() {
150        if (activeTest != null) {
151            if (!activeTest.isAlive()) {
152                /* Properly clean up on our last test */
153                try {
154                    activeTest.join();
155                }
156                catch (InterruptedException e) {
157                }
158                activeTest = null;
159            }
160        }
161
162        if (!stopTesting && activeTest == null) {
163            if (test_iter.hasNext()) {
164                activeTest = test_iter.next();
165                activeTest.start();
166                /* This routine will only get called once when a new test
167                 * should start running. The message handler in UnitTest.java
168                 * ensures this. */
169            }
170            else {
171                if (mTimer != null) {
172                    mTimer.cancel();
173                    mTimer.purge();
174                    mTimer = null;
175                }
176            }
177        }
178    }
179
180    public void refreshTestResults() {
181        checkAndRunNextTest();
182
183        if (mListAllocs != null && mScript != null && mRS != null) {
184            mListAllocs.copyAll();
185
186            mScript.bind_gList(mListAllocs);
187            mRS.bindRootScript(mScript);
188        }
189    }
190
191    public void cleanup() {
192        stopTesting = true;
193        UnitTest t = activeTest;
194
195        /* Stop periodic refresh of testing */
196        if (mTimer != null) {
197            mTimer.cancel();
198            mTimer.purge();
199            mTimer = null;
200        }
201
202        /* Wait to exit until we finish the current test */
203        if (t != null) {
204            try {
205                t.join();
206            }
207            catch (InterruptedException e) {
208            }
209            t = null;
210        }
211
212    }
213
214    public void newTouchPosition(float x, float y, float pressure, int id) {
215    }
216
217    public void onActionDown(int x, int y) {
218        mScript.set_gDY(0.0f);
219        mLastX = x;
220        mLastY = y;
221        refreshTestResults();
222    }
223
224    public void onActionMove(int x, int y) {
225        int dx = mLastX - x;
226        int dy = mLastY - y;
227
228        if (Math.abs(dy) <= 2) {
229            dy = 0;
230        }
231
232        mScript.set_gDY(dy);
233
234        mLastX = x;
235        mLastY = y;
236        refreshTestResults();
237    }
238}
239