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 18package android.filterpacks.imageproc; 19 20import android.filterfw.core.Filter; 21import android.filterfw.core.FilterContext; 22import android.filterfw.core.Frame; 23import android.filterfw.core.FrameFormat; 24import android.filterfw.core.KeyValueMap; 25import android.filterfw.core.NativeProgram; 26import android.filterfw.core.NativeFrame; 27import android.filterfw.core.Program; 28import android.filterfw.core.ShaderProgram; 29import android.filterfw.format.ImageFormat; 30 31import java.lang.reflect.Field; 32import java.util.HashSet; 33import java.util.Set; 34 35/** 36 * @hide 37 */ 38public abstract class SimpleImageFilter extends Filter { 39 40 protected int mCurrentTarget = FrameFormat.TARGET_UNSPECIFIED; 41 protected Program mProgram; 42 protected String mParameterName; 43 44 public SimpleImageFilter(String name, String parameterName) { 45 super(name); 46 mParameterName = parameterName; 47 } 48 49 @Override 50 public void setupPorts() { 51 if (mParameterName != null) { 52 try { 53 Field programField = SimpleImageFilter.class.getDeclaredField("mProgram"); 54 addProgramPort(mParameterName, mParameterName, programField, float.class, false); 55 } catch (NoSuchFieldException e) { 56 throw new RuntimeException("Internal Error: mProgram field not found!"); 57 } 58 } 59 addMaskedInputPort("image", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 60 addOutputBasedOnInput("image", "image"); 61 } 62 63 @Override 64 public FrameFormat getOutputFormat(String portName, FrameFormat inputFormat) { 65 return inputFormat; 66 } 67 68 @Override 69 public void process(FilterContext context) { 70 // Get input frame 71 Frame input = pullInput("image"); 72 FrameFormat inputFormat = input.getFormat(); 73 74 // Create output frame 75 Frame output = context.getFrameManager().newFrame(inputFormat); 76 77 // Create program if not created already 78 updateProgramWithTarget(inputFormat.getTarget(), context); 79 80 // Process 81 mProgram.process(input, output); 82 83 // Push output 84 pushOutput("image", output); 85 86 // Release pushed frame 87 output.release(); 88 } 89 90 protected void updateProgramWithTarget(int target, FilterContext context) { 91 if (target != mCurrentTarget) { 92 switch (target) { 93 case FrameFormat.TARGET_NATIVE: 94 mProgram = getNativeProgram(context); 95 break; 96 97 case FrameFormat.TARGET_GPU: 98 mProgram = getShaderProgram(context); 99 break; 100 101 default: 102 mProgram = null; 103 break; 104 } 105 if (mProgram == null) { 106 throw new RuntimeException("Could not create a program for image filter " + this + "!"); 107 } 108 initProgramInputs(mProgram, context); 109 mCurrentTarget = target; 110 } 111 } 112 113 protected abstract Program getNativeProgram(FilterContext context); 114 115 protected abstract Program getShaderProgram(FilterContext context); 116} 117