SurfaceTextureTarget.java revision b5af71f2b108607149032ce9817c5897b67b4032
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 (mLogVerbose) Log.v(TAG, "updateRenderMode. Thread: " + Thread.currentThread()); 125 if (mRenderModeString != null) { 126 if (mRenderModeString.equals("stretch")) { 127 mRenderMode = RENDERMODE_STRETCH; 128 } else if (mRenderModeString.equals("fit")) { 129 mRenderMode = RENDERMODE_FIT; 130 } else if (mRenderModeString.equals("fill_crop")) { 131 mRenderMode = RENDERMODE_FILL_CROP; 132 } else if (mRenderModeString.equals("customize")) { 133 mRenderMode = RENDERMODE_CUSTOMIZE; 134 } else { 135 throw new RuntimeException("Unknown render mode '" + mRenderModeString + "'!"); 136 } 137 } 138 updateTargetRect(); 139 } 140 141 @Override 142 public void prepare(FilterContext context) { 143 if (mLogVerbose) Log.v(TAG, "Prepare. Thread: " + Thread.currentThread()); 144 // Create identity shader to render, and make sure to render upside-down, as textures 145 // are stored internally bottom-to-top. 146 mProgram = ShaderProgram.createIdentity(context); 147 mProgram.setSourceRect(0, 1, 1, -1); 148 mProgram.setClearColor(0.0f, 0.0f, 0.0f); 149 150 updateRenderMode(); 151 152 // Create a frame representing the screen 153 MutableFrameFormat screenFormat = new MutableFrameFormat(FrameFormat.TYPE_BYTE, 154 FrameFormat.TARGET_GPU); 155 screenFormat.setBytesPerSample(4); 156 screenFormat.setDimensions(mScreenWidth, mScreenHeight); 157 mScreen = (GLFrame)context.getFrameManager().newBoundFrame(screenFormat, 158 GLFrame.EXISTING_FBO_BINDING, 159 0); 160 } 161 162 @Override 163 public synchronized void open(FilterContext context) { 164 // Set up SurfaceTexture internals 165 mSurfaceId = context.getGLEnvironment().registerSurfaceTexture( 166 mSurfaceTexture, mScreenWidth, mScreenHeight); 167 if (mSurfaceId <= 0) { 168 throw new RuntimeException("Could not register SurfaceTexture: " + mSurfaceTexture); 169 } 170 } 171 172 173 @Override 174 public synchronized void close(FilterContext context) { 175 if (mSurfaceId > 0) { 176 context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); 177 mSurfaceId = -1; 178 // Once the surface is unregistered, remove the surfacetexture reference. 179 // The surfaceId could not have been valid without a valid surfacetexture. 180 mSurfaceTexture = null; 181 } 182 } 183 184 // This should be called from the client side when the surfacetexture is no longer 185 // valid. e.g. from onPause() in the application using the filter graph. 186 public synchronized void disconnect(FilterContext context) { 187 if (mLogVerbose) Log.v(TAG, "disconnect"); 188 if (mSurfaceTexture == null) { 189 Log.d(TAG, "SurfaceTexture is already null. Nothing to disconnect."); 190 return; 191 } 192 mSurfaceTexture = null; 193 // Make sure we unregister the surface as well if a surface was registered. 194 // There can be a situation where the surface was not registered but the 195 // surfacetexture was valid. For example, the disconnect can be called before 196 // the filter was opened. Hence, the surfaceId may not be a valid one here, 197 // and need to check for its validity. 198 if (mSurfaceId > 0) { 199 context.getGLEnvironment().unregisterSurfaceId(mSurfaceId); 200 mSurfaceId = -1; 201 } 202 } 203 204 @Override 205 public synchronized void process(FilterContext context) { 206 // Surface is not registered. Nothing to render into. 207 if (mSurfaceId <= 0) { 208 return; 209 } 210 GLEnvironment glEnv = context.getGLEnvironment(); 211 212 // Get input frame 213 Frame input = pullInput("frame"); 214 boolean createdFrame = false; 215 216 float currentAspectRatio = 217 (float)input.getFormat().getWidth() / input.getFormat().getHeight(); 218 if (currentAspectRatio != mAspectRatio) { 219 if (mLogVerbose) { 220 Log.v(TAG, "Process. New aspect ratio: " + currentAspectRatio + 221 ", previously: " + mAspectRatio + ". Thread: " + Thread.currentThread()); 222 } 223 mAspectRatio = currentAspectRatio; 224 updateTargetRect(); 225 } 226 227 // See if we need to copy to GPU 228 Frame gpuFrame = null; 229 int target = input.getFormat().getTarget(); 230 if (target != FrameFormat.TARGET_GPU) { 231 gpuFrame = context.getFrameManager().duplicateFrameToTarget(input, 232 FrameFormat.TARGET_GPU); 233 createdFrame = true; 234 } else { 235 gpuFrame = input; 236 } 237 238 // Activate our surface 239 glEnv.activateSurfaceWithId(mSurfaceId); 240 241 // Process 242 mProgram.process(gpuFrame, mScreen); 243 244 glEnv.setSurfaceTimestamp(input.getTimestamp()); 245 246 // And swap buffers 247 glEnv.swapBuffers(); 248 249 if (createdFrame) { 250 gpuFrame.release(); 251 } 252 } 253 254 @Override 255 public void fieldPortValueUpdated(String name, FilterContext context) { 256 if (mLogVerbose) Log.v(TAG, "FPVU. Thread: " + Thread.currentThread()); 257 updateRenderMode(); 258 } 259 260 @Override 261 public void tearDown(FilterContext context) { 262 if (mScreen != null) { 263 mScreen.release(); 264 } 265 } 266 267 private void updateTargetRect() { 268 if (mLogVerbose) Log.v(TAG, "updateTargetRect. Thread: " + Thread.currentThread()); 269 if (mScreenWidth > 0 && mScreenHeight > 0 && mProgram != null) { 270 float screenAspectRatio = (float)mScreenWidth / mScreenHeight; 271 float relativeAspectRatio = screenAspectRatio / mAspectRatio; 272 if (mLogVerbose) { 273 Log.v(TAG, "UTR. screen w = " + (float)mScreenWidth + " x screen h = " + 274 (float)mScreenHeight + " Screen AR: " + screenAspectRatio + 275 ", frame AR: " + mAspectRatio + ", relative AR: " + relativeAspectRatio); 276 } 277 278 if (relativeAspectRatio == 1.0f && mRenderMode != RENDERMODE_CUSTOMIZE) { 279 mProgram.setTargetRect(0, 0, 1, 1); 280 mProgram.setClearsOutput(false); 281 } else { 282 switch (mRenderMode) { 283 case RENDERMODE_STRETCH: 284 mTargetQuad.p0.set(0f, 0.0f); 285 mTargetQuad.p1.set(1f, 0.0f); 286 mTargetQuad.p2.set(0f, 1.0f); 287 mTargetQuad.p3.set(1f, 1.0f); 288 mProgram.setClearsOutput(false); 289 break; 290 case RENDERMODE_FIT: 291 if (relativeAspectRatio > 1.0f) { 292 // Screen is wider than the camera, scale down X 293 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 294 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 295 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 296 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 297 298 } else { 299 // Screen is taller than the camera, scale down Y 300 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 301 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 302 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 303 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 304 } 305 mProgram.setClearsOutput(true); 306 break; 307 case RENDERMODE_FILL_CROP: 308 if (relativeAspectRatio > 1) { 309 // Screen is wider than the camera, crop in Y 310 mTargetQuad.p0.set(0.0f, 0.5f - 0.5f * relativeAspectRatio); 311 mTargetQuad.p1.set(1.0f, 0.5f - 0.5f * relativeAspectRatio); 312 mTargetQuad.p2.set(0.0f, 0.5f + 0.5f * relativeAspectRatio); 313 mTargetQuad.p3.set(1.0f, 0.5f + 0.5f * relativeAspectRatio); 314 } else { 315 // Screen is taller than the camera, crop in X 316 mTargetQuad.p0.set(0.5f - 0.5f / relativeAspectRatio, 0.0f); 317 mTargetQuad.p1.set(0.5f + 0.5f / relativeAspectRatio, 0.0f); 318 mTargetQuad.p2.set(0.5f - 0.5f / relativeAspectRatio, 1.0f); 319 mTargetQuad.p3.set(0.5f + 0.5f / relativeAspectRatio, 1.0f); 320 } 321 mProgram.setClearsOutput(true); 322 break; 323 case RENDERMODE_CUSTOMIZE: 324 ((ShaderProgram) mProgram).setSourceRegion(mSourceQuad); 325 break; 326 } 327 if (mLogVerbose) Log.v(TAG, "UTR. quad: " + mTargetQuad); 328 ((ShaderProgram) mProgram).setTargetRegion(mTargetQuad); 329 } 330 } 331 } 332} 333