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.Allocation;
20 import android.renderscript.Element;
21 import android.renderscript.RenderScript;
22 import android.renderscript.Type;
23 
24 public class GlobalSync extends RSBaseCompute {
25     Allocation AFailed;
26     int [] Failed;
27     Allocation AIn;
28 
setupGlobalSync(RenderScript mRS, ScriptC_global_sync gs, int v)29     protected void setupGlobalSync(RenderScript mRS, ScriptC_global_sync gs, int v) {
30         Type.Builder typeBuilder = new Type.Builder(mRS, Element.I32(mRS));
31         Type t = typeBuilder.setX(1).create();
32 
33         AFailed = Allocation.createTyped(mRS, t);
34         Failed = new int [1];
35         Failed[0] = 0;
36         AFailed.copyFrom(Failed);
37         gs.set_aFailed(AFailed);
38 
39         AIn = Allocation.createTyped(mRS, t);
40         int [] In = new int [1];
41         In[0] = v;
42         AIn.copyFrom(In);
43     }
44 
45     @Override
tearDown()46     protected void tearDown() throws Exception {
47         if (AFailed != null) {
48             AFailed.destroy();
49         }
50         if (AIn != null) {
51             AIn.destroy();
52         }
53         super.tearDown();
54     }
55 
56     /**
57      * Test whether we are properly synchronizing extern global data
58      * when going from kernel to invokable.
59      */
testKIGlobalSync()60     public void testKIGlobalSync() {
61         ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
62 
63         int v = 7;
64         setupGlobalSync(mRS, gs, v);
65         gs.forEach_write_global(AIn);
66         gs.invoke_test_read_global(v);
67 
68         AFailed.copyTo(Failed);
69         if (Failed[0] != 0) {
70             FoundError = true;
71         }
72 
73         gs.destroy();
74         checkForErrors();
75     }
76 
77     /**
78      * Test whether we are properly synchronizing static global data
79      * when going from invokable to kernel.
80      */
testKIStaticGlobalSync()81     public void testKIStaticGlobalSync() {
82         ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
83 
84         int v = 9;
85         setupGlobalSync(mRS, gs, v);
86         gs.forEach_write_static_global(AIn);
87         gs.invoke_test_read_static_global(v);
88 
89         AFailed.copyTo(Failed);
90         if (Failed[0] != 0) {
91             FoundError = true;
92         }
93 
94         gs.destroy();
95         checkForErrors();
96     }
97 
98     /**
99      * Test whether we are properly synchronizing extern global data
100      * when going from invokable to kernel.
101      */
testIKGlobalSync()102     public void testIKGlobalSync() {
103         ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
104 
105         int v = 7;
106         setupGlobalSync(mRS, gs, v);
107         gs.invoke_test_write_global(v);
108         gs.forEach_read_global(AIn, AFailed);
109 
110         AFailed.copyTo(Failed);
111         if (Failed[0] != 0) {
112             FoundError = true;
113         }
114 
115         gs.destroy();
116         checkForErrors();
117     }
118 
119     /**
120      * Test whether we are properly synchronizing static global data
121      * when going from kernel to invokable.
122      */
testIKStaticGlobalSync()123     public void testIKStaticGlobalSync() {
124         ScriptC_global_sync gs = new ScriptC_global_sync(mRS);
125 
126         int v = 9;
127         setupGlobalSync(mRS, gs, v);
128         gs.invoke_test_write_static_global(v);
129         gs.forEach_read_static_global(AIn, AFailed);
130 
131         AFailed.copyTo(Failed);
132         if (Failed[0] != 0) {
133             FoundError = true;
134         }
135 
136         gs.destroy();
137         checkForErrors();
138     }
139 }
140