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