From 41ce6c2402fd9a4eed0236e22dced9d5ef761f4a Mon Sep 17 00:00:00 2001 From: Marius Renn Date: Thu, 30 Jun 2011 17:18:53 -0700 Subject: [PATCH] Minor refactoring to GraphRunners, so that graphs can be partially run. Change-Id: Ife07d451bee5238774a16c763c02ada7f949de6d --- .../java/android/filterfw/core/AsyncRunner.java | 29 ++++++++++++------- .../java/android/filterfw/core/GraphRunner.java | 2 ++ .../java/android/filterfw/core/SyncRunner.java | 33 +++++++++------------- 3 files changed, 35 insertions(+), 29 deletions(-) diff --git a/mca/filterfw/java/android/filterfw/core/AsyncRunner.java b/mca/filterfw/java/android/filterfw/core/AsyncRunner.java index d8e98ddc..5e185707 100644 --- a/mca/filterfw/java/android/filterfw/core/AsyncRunner.java +++ b/mca/filterfw/java/android/filterfw/core/AsyncRunner.java @@ -66,29 +66,38 @@ public class AsyncRunner extends GraphRunner{ throw new RuntimeException("More than one callback data set received!"); } + // Preparation if (mLogVerbose) Log.v(TAG, "Starting background graph processing."); activateGlContext(); - boolean isDone = false; if (mLogVerbose) Log.v(TAG, "Preparing filter graph for processing."); runner[0].beginProcessing(); if (mLogVerbose) Log.v(TAG, "Running graph."); runner[0].assertReadyToStep(); - while (!isCancelled() && !isDone) { - int status = runner[0].performStep(true); - if (status == GraphRunner.RESULT_SLEEPING) { - runner[0].waitUntilWake(); - } else if (status != GraphRunner.RESULT_RUNNING) { - isDone = true; + + // Run loop + int status = RESULT_RUNNING; + while (!isCancelled() && status == RESULT_RUNNING) { + if (!runner[0].performStep()) { + status = runner[0].determinePostRunState(); + if (status == GraphRunner.RESULT_SLEEPING) { + runner[0].waitUntilWake(); + status = RESULT_RUNNING; + } } } - if (mLogVerbose) Log.v(TAG, "Closing filters."); - runner[0].close(); + + // Cleanup + if (isCancelled()) { + if (mLogVerbose) Log.v(TAG, "Closing filters."); + runner[0].close(); + } + deactivateGlContext(); if (mLogVerbose) Log.v(TAG, "Done with background graph processing."); - return isDone ? RESULT_FINISHED : RESULT_STOPPED; + return status; } @Override diff --git a/mca/filterfw/java/android/filterfw/core/GraphRunner.java b/mca/filterfw/java/android/filterfw/core/GraphRunner.java index 3ec65b23..1fb96a10 100644 --- a/mca/filterfw/java/android/filterfw/core/GraphRunner.java +++ b/mca/filterfw/java/android/filterfw/core/GraphRunner.java @@ -36,6 +36,7 @@ public abstract class GraphRunner { public void onRunnerDone(int result); } + public static final int RESULT_UNKNOWN = 0; public static final int RESULT_RUNNING = 1; public static final int RESULT_FINISHED = 2; public static final int RESULT_SLEEPING = 3; @@ -78,4 +79,5 @@ public abstract class GraphRunner { public abstract boolean isRunning(); public abstract void stop(); + } diff --git a/mca/filterfw/java/android/filterfw/core/SyncRunner.java b/mca/filterfw/java/android/filterfw/core/SyncRunner.java index 411dc416..0c31fb48 100644 --- a/mca/filterfw/java/android/filterfw/core/SyncRunner.java +++ b/mca/filterfw/java/android/filterfw/core/SyncRunner.java @@ -81,7 +81,7 @@ public class SyncRunner extends GraphRunner { public int step() { assertReadyToStep(); - return performStep(true); + return performStep() ? RESULT_RUNNING : determinePostRunState(); } public void beginProcessing() { @@ -101,18 +101,17 @@ public class SyncRunner extends GraphRunner { activateGlContext(); // Run - int status = RESULT_RUNNING; - while (status == RESULT_RUNNING) { - status = performStep(false); + boolean keepRunning = true; + while (keepRunning) { + keepRunning = performStep(); } - // Close - close(); + // Cleanup deactivateGlContext(); // Call completion callback if set if (mDoneListener != null) { - mDoneListener.onRunnerDone(status); + mDoneListener.onRunnerDone(determinePostRunState()); } } @@ -131,15 +130,11 @@ public class SyncRunner extends GraphRunner { throw new RuntimeException("SyncRunner does not support stopping a graph!"); } - public int getGraphState() { - return determineGraphState(); - } - - public void waitUntilWake() { + protected void waitUntilWake() { mWakeCondition.block(); } - private void processFilterNode(Filter filter) { + protected void processFilterNode(Filter filter) { LogMCA.perFrame("Processing " + filter); filter.performProcess(mFilterContext); if (filter.getStatus() == Filter.STATUS_ERROR) { @@ -149,7 +144,7 @@ public class SyncRunner extends GraphRunner { } } - private void scheduleFilterWake(Filter filter, int delay) { + protected void scheduleFilterWake(Filter filter, int delay) { // Close the wake condition mWakeCondition.close(); @@ -166,7 +161,7 @@ public class SyncRunner extends GraphRunner { }, delay, TimeUnit.MILLISECONDS); } - private int determineGraphState() { + protected int determinePostRunState() { boolean isBlocked = false; for (Filter filter : mScheduler.getGraph().getFilters()) { if (filter.isOpen()) { @@ -174,7 +169,7 @@ public class SyncRunner extends GraphRunner { // If ANY node is sleeping, we return our state as sleeping return RESULT_SLEEPING; } else { - // TODO: Be more precise whether this is blocked or starved + // If a node is still open, it is blocked (by input or output) return RESULT_BLOCKED; } } @@ -183,15 +178,15 @@ public class SyncRunner extends GraphRunner { } // Core internal methods /////////////////////////////////////////////////////////////////////// - int performStep(boolean detailedState) { + boolean performStep() { Filter filter = mScheduler.scheduleNextNode(); if (filter != null) { mTimer.start(filter.getName()); processFilterNode(filter); mTimer.stop(filter.getName()); - return RESULT_RUNNING; + return true; } else { - return detailedState ? determineGraphState() : RESULT_FINISHED; + return false; } } -- 2.11.0