SurfaceTextureTarget.java revision 65953da4636fbf5f0a24b8f5f2b5fa7d76ff13d9
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.videosrc; 19 20import android.filterfw.core.Filter; 21import android.filterfw.core.FilterContext; 22import android.filterfw.core.FilterSurfaceView; 23import android.filterfw.core.Frame; 24import android.filterfw.core.FrameFormat; 25import android.filterfw.core.GenerateFieldPort; 26import android.filterfw.core.GenerateFinalPort; 27import android.filterfw.core.GLEnvironment; 28import android.filterfw.core.GLFrame; 29import android.filterfw.core.KeyValueMap; 30import android.filterfw.core.MutableFrameFormat; 31import android.filterfw.core.NativeProgram; 32import android.filterfw.core.NativeFrame; 33import android.filterfw.core.Program; 34import android.filterfw.core.ShaderProgram; 35import android.filterfw.format.ImageFormat; 36 37import android.filterfw.geometry.Quad; 38import android.filterfw.geometry.Point; 39 40import android.view.Surface; 41import android.view.SurfaceHolder; 42import android.view.SurfaceView; 43 44import android.graphics.Rect; 45import android.graphics.SurfaceTexture; 46 47import android.util.Log; 48 49/** 50 * @hide 51 */ 52public class SurfaceTextureTarget extends Filter { 53 54 private final int RENDERMODE_STRETCH = 0; 55 private final int RENDERMODE_FIT = 1; 56 private final int RENDERMODE_FILL_CROP = 2; 57 private final int RENDERMODE_CUSTOMIZE = 3; 58 59 /** Required. Sets the destination surfaceTexture. 60 */ 61 @GenerateFinalPort(name = "surfaceTexture") 62 private SurfaceTexture mSurfaceTexture; 63 64 /** Required. Sets the width of the output surfaceTexture images */ 65 @GenerateFinalPort(name = "width") 66 private int mScreenWidth; 67 68 /** Required. Sets the height of the output surfaceTexture images */ 69 @GenerateFinalPort(name = "height") 70 private int mScreenHeight; 71 72 73 /** Optional. Control how the incoming frames are rendered onto the 74 * output. Default is FIT. 75 * RENDERMODE_STRETCH: Just fill the output surfaceView. 76 * RENDERMODE_FIT: Keep aspect ratio and fit without cropping. May 77 * have black bars. 78 * RENDERMODE_FILL_CROP: Keep aspect ratio and fit without black 79 * bars. May crop. 80 */ 81 @GenerateFieldPort(name = "renderMode", hasDefault = true) 82 private String mRenderModeString; 83 84 @GenerateFieldPort(name = "sourceQuad", hasDefault = true) 85 private Quad mSourceQuad = new Quad(new Point(0.0f, 1.0f), 86 new Point(1.0f, 1.0f), 87 new Point(0.0f, 0.0f), 88 new Point(1.0f, 0.0f)); 89 90 @GenerateFieldPort(name = "targetQuad", hasDefault = true) 91 private Quad mTargetQuad = new Quad(new Point(0.0f, 0.0f), 92 new Point(1.0f, 0.0f), 93 new Point(0.0f, 1.0f), 94 new Point(1.0f, 1.0f)); 95 96 private int mSurfaceId; 97 98 private ShaderProgram mProgram; 99 private GLFrame mScreen; 100 private int mRenderMode = RENDERMODE_FIT; 101 private float mAspectRatio = 1.f; 102 103 private boolean mLogVerbose; 104 private static final String TAG = "SurfaceTextureTarget"; 105 106 public SurfaceTextureTarget(String name) { 107 super(name); 108 109 mLogVerbose = Log.isLoggable(TAG, Log.VERBOSE); 110 } 111 112 @Override 113 public void setupPorts() { 114 // Make sure we have a SurfaceView 115 if (mSurfaceTexture == null) { 116 throw new RuntimeException("Null SurfaceTexture passed to SurfaceTextureTarget"); 117 } 118 119 // Add input port - will accept anything that's 4-channel. 120 addMaskedInputPort("frame", ImageFormat.create(ImageFormat.COLORSPACE_RGBA)); 121 } 122 123 public void updateRenderMode() { 124 if (mRenderModeString != null) { 125 if (mRenderModeString.equals("stretch")) { 126 mRenderMode = RENDERMODE_STRETCH; 127 } else if (mRenderModeString.equals("fit")) { 128 mRenderMode = RENDERMODE_FIT; 129 } else if (mRenderModeString.equals("fill_crop")) { 130 mRenderMode = RENDERMODE_FILL_CROP; 131 } else if (mRenderModeString.equals("customize")) { 132 mRenderMode = RENDERMODE_CUSTOMIZE; 133 } else { 134 throw new RuntimeException("Unknown render mode '" + mRenderModeString + "'!"); 135 } 136 } 137 updateTargetRect(); 138 } 139 140 @Override 141 public void prepare(FilterContext context) { 142 // Create identity shader to render, and make sure to render upside-down, as textures 143 // are stored internally bottom-to-top. 144 mProgram = ShaderProgram.createIdentity(context); 145 mProgram.setSourceRect(0, 1, 1, -1); 146 mProgram.setClearColor(0.0f, 0.0f, 0.0f); 147 148 updateRenderMode(); 149 150 // Create a frame representing the screen 151 MutableFrameFormat screenFormat = new MutableFrameFormat(FrameFormat.TYPE_BYTE, 152 FrameFormat.TARGET_GPU); 153 screenFormat.setBytesPerSample(4); 154 screenFormat.setDimensions(mScreenWidth, mScreenHeight); 155 mScreen = (GLFrame)context.getFrameManager().newBoundFrame(screenFormat, 156 GLFrame.EXISTING_FBO_BINDING, 157 0); 158 } 159 160 @Override 161 public void open(FilterContext context) { 162 // Set up SurfaceTexture internals 163 mSurfaceId = context.getGLEnvironment().registerSurfaceTexture(mSurfaceTexture, mScreenWidth, mScreenHeight); 164 } 165 166 @Override 167 public void process(FilterContext context) { 168 if (mLogVerbose) Log.v(TAG, "Starting frame processing"); 169 170 GLEnvironment glEnv = context.getGLEnvironment(); 171 172 // Get input frame 173 Frame input = pullInput("frame"); 174 boolean createdFrame = false; 175 176 float currentAspectRatio = (float)input.getFormat().getWidth() / input.getFormat().getHeight(); 177 if (currentAspectRatio != mAspectRatio) { 178 if (mLogVerbose) Log.v(TAG, "New aspect ratio: " + currentAspectRatio +", previously: " + mAspectRatio); 179 mAspectRatio = currentAspectRatio; 180 updateTargetRect(); 181 } 182 183 // See if we need to copy to GPU 184 Frame gpuFrame = null; 185 if (mLogVerbose) Log.v("SurfaceTextureTarget", "Got input format: " + input.getFormat()); 186 187 int target = input.getFormat().getTarget(); 188 if (target != FrameFormat.TARGET_GPU) { 189 gpuFrame = context.getFrameManager().duplicateFrameToTarget(input, 190 FrameFormat.TARGET_GPU); 191 createdFrame = true; 192 } else { 193 gpuFrame = input; 194 } 195 196 // Activate our surface 197 glEnv.activateSurfaceWithId(mSurfaceId); 198 199 // Process 200 mProgram.process(gpuFrame, mScreen); 201 202 glEnv.setSurfaceTimestamp(input.getTimestamp()); 203 204 // And swap buffers 205 glEnv.swapBuffers(); 206 207 if (createdFrame) { 208 gpuFrame.release(); 209 } 210 } 211 212 @Override 213 public void fieldPortValueUpdated(String name, FilterContext context) { 214 updateRenderMode(); 215 } 216 217 @Override 218 public void tearDown(FilterContext context) { 219 if (mScreen != null) { 220 mScreen.release(); 221 } 222 } 223 224 private void updateTargetRect() { 225 if (mScreenWidth > 0 && mScreenHeight > 0 && mProgram != null) { 226 float screenAspectRatio = (float)mScreenWidth / mScreenHeight; 227 float relativeAspectRatio = screenAspectRatio / mAspectRatio; 228 229 if (relativeAspectRatio == 1.0f && mRenderMode != RENDERMODE_CUSTOMIZE) { 230 mProgram.setClearsOutput(false); 231 } else { 232 switch (mRenderMode) { 233 case RENDERMODE_STRETCH: 234 mProgram.setTargetRect(0, 0, 1, 1); 235 mTargetQuad.p0.set(0f, 0.0f); 236 mTargetQuad.p1.set(1f, 0.0f); 237 mTargetQuad.p2.set(0f, 1.0f); 238 mTargetQuad.p3.set(1f, 1.0f); 239 mProgram.setClearsOutput(false); 240 break; 241 case RENDERMODE_FIT: 242 if (relativeAspectRatio > 1.0f) { 243 // Screen is wider than the camera, scale down X 244 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 245 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 246 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 247 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 248 249 } else { 250 // Screen is taller than the camera, scale down Y 251 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 252 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 253 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 254 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 255 } 256 mProgram.setClearsOutput(true); 257 break; 258 case RENDERMODE_FILL_CROP: 259 if (relativeAspectRatio > 1) { 260 // Screen is wider than the camera, crop in Y 261 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 262 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 263 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 264 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 265 } else { 266 // Screen is taller than the camera, crop in X 267 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 268 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 269 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 270 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 271 } 272 mProgram.setClearsOutput(true); 273 break; 274 case RENDERMODE_CUSTOMIZE: 275 ((ShaderProgram) mProgram).setSourceRegion(mSourceQuad); 276 break; 277 } 278 ((ShaderProgram) mProgram).setTargetRegion(mTargetQuad); 279 } 280 } 281 } 282} 283