SurfaceTextureTarget.java revision b939760679caa9fdd06c862cf8218cc8f4a90ef1
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 synchronized 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 synchronized void open(FilterContext context) { 162 // Set up SurfaceTexture internals 163 mSurfaceId = context.getGLEnvironment().registerSurfaceTexture( 164 mSurfaceTexture, mScreenWidth, mScreenHeight); 165 if (mSurfaceId <= 0) { 166 throw new RuntimeException("Could not register SurfaceTexture: " + mSurfaceTexture); 167 } 168 } 169 170 171 @Override 172 public synchronized void close(FilterContext context) { 173 if (mSurfaceId > 0) { 174 context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); 175 mSurfaceId = -1; 176 // Once the surface is unregistered, remove the surfacetexture reference. 177 // The surfaceId could not have been valid without a valid surfacetexture. 178 mSurfaceTexture = null; 179 } 180 } 181 182 // This should be called from the client side when the surfacetexture is no longer 183 // valid. e.g. from onPause() in the application using the filter graph. 184 public synchronized void disconnect(FilterContext context) { 185 if (mLogVerbose) Log.v(TAG, "disconnect"); 186 if (mSurfaceTexture == null) { 187 Log.d(TAG, "SurfaceTexture is already null. Nothing to disconnect."); 188 return; 189 } 190 mSurfaceTexture = null; 191 // Make sure we unregister the surface as well if a surface was registered. 192 // There can be a situation where the surface was not registered but the 193 // surfacetexture was valid. For example, the disconnect can be called before 194 // the filter was opened. Hence, the surfaceId may not be a valid one here, 195 // and need to check for its validity. 196 if (mSurfaceId > 0) { 197 context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); 198 mSurfaceId = -1; 199 } 200 } 201 202 @Override 203 public synchronized void process(FilterContext context) { 204 // Surface is not registered. Nothing to render into. 205 if (mSurfaceId <= 0) { 206 return; 207 } 208 GLEnvironment glEnv = context.getGLEnvironment(); 209 210 // Get input frame 211 Frame input = pullInput("frame"); 212 boolean createdFrame = false; 213 214 float currentAspectRatio = 215 (float)input.getFormat().getWidth() / input.getFormat().getHeight(); 216 if (currentAspectRatio != mAspectRatio) { 217 if (mLogVerbose) Log.v(TAG, "New aspect ratio: " + currentAspectRatio + 218 ", previously: " + mAspectRatio); 219 mAspectRatio = currentAspectRatio; 220 updateTargetRect(); 221 } 222 223 // See if we need to copy to GPU 224 Frame gpuFrame = null; 225 int target = input.getFormat().getTarget(); 226 if (target != FrameFormat.TARGET_GPU) { 227 gpuFrame = context.getFrameManager().duplicateFrameToTarget(input, 228 FrameFormat.TARGET_GPU); 229 createdFrame = true; 230 } else { 231 gpuFrame = input; 232 } 233 234 // Activate our surface 235 glEnv.activateSurfaceWithId(mSurfaceId); 236 237 // Process 238 mProgram.process(gpuFrame, mScreen); 239 240 glEnv.setSurfaceTimestamp(input.getTimestamp()); 241 242 // And swap buffers 243 glEnv.swapBuffers(); 244 245 if (createdFrame) { 246 gpuFrame.release(); 247 } 248 } 249 250 @Override 251 public void fieldPortValueUpdated(String name, FilterContext context) { 252 updateRenderMode(); 253 } 254 255 @Override 256 public void tearDown(FilterContext context) { 257 if (mScreen != null) { 258 mScreen.release(); 259 } 260 } 261 262 private void updateTargetRect() { 263 if (mScreenWidth > 0 && mScreenHeight > 0 && mProgram != null) { 264 float screenAspectRatio = (float)mScreenWidth / mScreenHeight; 265 float relativeAspectRatio = screenAspectRatio / mAspectRatio; 266 267 if (relativeAspectRatio == 1.0f && mRenderMode != RENDERMODE_CUSTOMIZE) { 268 mProgram.setClearsOutput(false); 269 } else { 270 switch (mRenderMode) { 271 case RENDERMODE_STRETCH: 272 mProgram.setTargetRect(0, 0, 1, 1); 273 mTargetQuad.p0.set(0f, 0.0f); 274 mTargetQuad.p1.set(1f, 0.0f); 275 mTargetQuad.p2.set(0f, 1.0f); 276 mTargetQuad.p3.set(1f, 1.0f); 277 mProgram.setClearsOutput(false); 278 break; 279 case RENDERMODE_FIT: 280 if (relativeAspectRatio > 1.0f) { 281 // Screen is wider than the camera, scale down X 282 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 283 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 284 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 285 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 286 287 } else { 288 // Screen is taller than the camera, scale down Y 289 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 290 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 291 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 292 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 293 } 294 mProgram.setClearsOutput(true); 295 break; 296 case RENDERMODE_FILL_CROP: 297 if (relativeAspectRatio > 1) { 298 // Screen is wider than the camera, crop in Y 299 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 300 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 301 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 302 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 303 } else { 304 // Screen is taller than the camera, crop in X 305 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 306 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 307 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 308 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 309 } 310 mProgram.setClearsOutput(true); 311 break; 312 case RENDERMODE_CUSTOMIZE: 313 ((ShaderProgram) mProgram).setSourceRegion(mSourceQuad); 314 break; 315 } 316 ((ShaderProgram) mProgram).setTargetRegion(mTargetQuad); 317 } 318 } 319 } 320} 321