1 /* 2 * Copyright 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 androidx.media.filterfw.samples.simplecamera; 18 19 import android.util.Log; 20 import androidx.media.filterfw.Filter; 21 import androidx.media.filterfw.FrameBuffer2D; 22 import androidx.media.filterfw.FrameImage2D; 23 import androidx.media.filterfw.FrameType; 24 import androidx.media.filterfw.FrameValue; 25 import androidx.media.filterfw.MffContext; 26 import androidx.media.filterfw.OutputPort; 27 import androidx.media.filterfw.Signature; 28 29 30 public class IfElseFilter extends Filter { 31 32 private static final String TAG = "IfElseFilter"; 33 private static boolean mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE); 34 35 public IfElseFilter(MffContext context, String name) { 36 super(context, name); 37 } 38 39 @Override 40 public Signature getSignature() { 41 FrameType imageIn = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_GPU); 42 FrameType videoIn = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.READ_GPU); 43 FrameType imageOut = FrameType.image2D(FrameType.ELEMENT_RGBA8888, FrameType.WRITE_GPU); 44 45 return new Signature().addInputPort("falseResult", Signature.PORT_REQUIRED, imageIn) 46 .addInputPort("trueResult", Signature.PORT_REQUIRED, videoIn) 47 .addInputPort("condition", Signature.PORT_REQUIRED, FrameType.single(boolean.class)) 48 .addOutputPort("output", Signature.PORT_REQUIRED, imageOut) 49 .disallowOtherPorts(); 50 } 51 52 @Override 53 protected void onProcess() { 54 OutputPort outPort = getConnectedOutputPort("output"); 55 FrameImage2D trueFrame = getConnectedInputPort("trueResult").pullFrame().asFrameImage2D(); 56 FrameImage2D falseFrame = getConnectedInputPort("falseResult").pullFrame().asFrameImage2D(); 57 FrameValue boolFrameValue = getConnectedInputPort("condition").pullFrame().asFrameValue(); 58 boolean condition = (Boolean) boolFrameValue.getValue(); 59 FrameBuffer2D outputFrame; 60 // If the condition is true, then we want to use the camera, else use the gallery 61 if (condition) { 62 outputFrame = trueFrame; 63 } else { 64 outputFrame = falseFrame; 65 } 66 outPort.pushFrame(outputFrame); 67 68 } 69 70 } 71