Skip to content

Commit 5675419

Browse files
Jason SamsAndroid (Google) Code Review
authored andcommitted
Merge "Fix cycle detection for SGs." into jb-mr1-dev
2 parents 10542ec + 091f7cc commit 5675419

File tree

1 file changed

+13
-29
lines changed

1 file changed

+13
-29
lines changed

graphics/java/android/renderscript/ScriptGroup.java

Lines changed: 13 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -77,7 +77,6 @@ static class Node {
7777
ArrayList<Script.KernelID> mKernels = new ArrayList<Script.KernelID>();
7878
ArrayList<ConnectLine> mInputs = new ArrayList<ConnectLine>();
7979
ArrayList<ConnectLine> mOutputs = new ArrayList<ConnectLine>();
80-
boolean mSeen;
8180
int dagNumber;
8281

8382
Node mNext;
@@ -176,39 +175,24 @@ public Builder(RenderScript rs) {
176175
mRS = rs;
177176
}
178177

179-
private void validateCycleRecurse(Node n, int depth) {
180-
n.mSeen = true;
181-
182-
//android.util.Log.v("RSR", " validateCycleRecurse outputCount " + n.mOutputs.size());
183-
for (int ct=0; ct < n.mOutputs.size(); ct++) {
184-
final ConnectLine cl = n.mOutputs.get(ct);
178+
// do a DFS from original node, looking for original node
179+
// any cycle that could be created must contain original node
180+
private void validateCycle(Node target, Node original) {
181+
for (int ct = 0; ct < target.mOutputs.size(); ct++) {
182+
final ConnectLine cl = target.mOutputs.get(ct);
185183
if (cl.mToK != null) {
186184
Node tn = findNode(cl.mToK.mScript);
187-
if (tn.mSeen) {
185+
if (tn.equals(original)) {
188186
throw new RSInvalidStateException("Loops in group not allowed.");
189187
}
190-
validateCycleRecurse(tn, depth + 1);
188+
validateCycle(tn, original);
191189
}
192190
if (cl.mToF != null) {
193191
Node tn = findNode(cl.mToF.mScript);
194-
if (tn.mSeen) {
192+
if (tn.equals(original)) {
195193
throw new RSInvalidStateException("Loops in group not allowed.");
196194
}
197-
validateCycleRecurse(tn, depth + 1);
198-
}
199-
}
200-
}
201-
202-
private void validateCycle() {
203-
//android.util.Log.v("RSR", "validateCycle");
204-
205-
for (int ct=0; ct < mNodes.size(); ct++) {
206-
for (int ct2=0; ct2 < mNodes.size(); ct2++) {
207-
mNodes.get(ct2).mSeen = false;
208-
}
209-
Node n = mNodes.get(ct);
210-
if (n.mInputs.size() == 0) {
211-
validateCycleRecurse(n, 0);
195+
validateCycle(tn, original);
212196
}
213197
}
214198
}
@@ -327,7 +311,7 @@ public Builder addConnection(Type t, Script.KernelID from, Script.FieldID to) {
327311

328312
Node nf = findNode(from);
329313
if (nf == null) {
330-
throw new RSInvalidStateException("From kernel not found.");
314+
throw new RSInvalidStateException("From script not found.");
331315
}
332316

333317
Node nt = findNode(to.mScript);
@@ -341,7 +325,7 @@ public Builder addConnection(Type t, Script.KernelID from, Script.FieldID to) {
341325
nf.mOutputs.add(cl);
342326
nt.mInputs.add(cl);
343327

344-
validateCycle();
328+
validateCycle(nf, nf);
345329
return this;
346330
}
347331

@@ -362,7 +346,7 @@ public Builder addConnection(Type t, Script.KernelID from, Script.KernelID to) {
362346

363347
Node nf = findNode(from);
364348
if (nf == null) {
365-
throw new RSInvalidStateException("From kernel not found.");
349+
throw new RSInvalidStateException("From script not found.");
366350
}
367351

368352
Node nt = findNode(to);
@@ -376,7 +360,7 @@ public Builder addConnection(Type t, Script.KernelID from, Script.KernelID to) {
376360
nf.mOutputs.add(cl);
377361
nt.mInputs.add(cl);
378362

379-
validateCycle();
363+
validateCycle(nf, nf);
380364
return this;
381365
}
382366

0 commit comments

Comments
 (0)