1 /* 2 * Copyright (C) 2011 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 18 package android.filterfw.core; 19 20 import android.compat.annotation.UnsupportedAppUsage; 21 22 /** 23 * @hide 24 */ 25 public abstract class GraphRunner { 26 27 protected FilterContext mFilterContext = null; 28 29 /** Interface for listeners waiting for the runner to complete. */ 30 public interface OnRunnerDoneListener { 31 /** Callback method to be called when the runner completes a 32 * {@link #run()} call. 33 * 34 * @param result will be RESULT_FINISHED if the graph finished running 35 * on its own, RESULT_STOPPED if the runner was stopped by a call 36 * to stop(), RESULT_BLOCKED if no filters could run due to lack 37 * of inputs or outputs or due to scheduling policies, and 38 * RESULT_SLEEPING if a filter node requested sleep. 39 */ onRunnerDone(int result)40 public void onRunnerDone(int result); 41 } 42 43 public static final int RESULT_UNKNOWN = 0; 44 public static final int RESULT_RUNNING = 1; 45 public static final int RESULT_FINISHED = 2; 46 public static final int RESULT_SLEEPING = 3; 47 public static final int RESULT_BLOCKED = 4; 48 public static final int RESULT_STOPPED = 5; 49 public static final int RESULT_ERROR = 6; 50 GraphRunner(FilterContext context)51 public GraphRunner(FilterContext context) { 52 mFilterContext = context; 53 } 54 55 @UnsupportedAppUsage getGraph()56 public abstract FilterGraph getGraph(); 57 getContext()58 public FilterContext getContext() { 59 return mFilterContext; 60 } 61 62 /** 63 * Helper function for subclasses to activate the GL environment before running. 64 * @return true, if the GL environment was activated. Returns false, if the GL environment 65 * was already active. 66 */ activateGlContext()67 protected boolean activateGlContext() { 68 GLEnvironment glEnv = mFilterContext.getGLEnvironment(); 69 if (glEnv != null && !glEnv.isActive()) { 70 glEnv.activate(); 71 return true; 72 } 73 return false; 74 } 75 76 /** 77 * Helper function for subclasses to deactivate the GL environment after running. 78 */ deactivateGlContext()79 protected void deactivateGlContext() { 80 GLEnvironment glEnv = mFilterContext.getGLEnvironment(); 81 if (glEnv != null) { 82 glEnv.deactivate(); 83 } 84 } 85 86 /** Starts running the graph. Will open the filters in the graph if they are not already open. */ 87 @UnsupportedAppUsage run()88 public abstract void run(); 89 90 @UnsupportedAppUsage setDoneCallback(OnRunnerDoneListener listener)91 public abstract void setDoneCallback(OnRunnerDoneListener listener); isRunning()92 public abstract boolean isRunning(); 93 94 /** Stops graph execution. As part of stopping, also closes the graph nodes. */ 95 @UnsupportedAppUsage stop()96 public abstract void stop(); 97 98 /** Closes the filters in a graph. Can only be called if the graph is not running. */ close()99 public abstract void close(); 100 101 /** 102 * Returns the last exception that happened during an asynchronous run. Returns null if 103 * there is nothing to report. 104 */ 105 @UnsupportedAppUsage getError()106 public abstract Exception getError(); 107 } 108