1 package com.example.helloandroidcamera2; 2 3 import android.view.Surface; 4 5 public class NativeSurfaceHandle implements AutoCloseable { 6 7 private long mHandle; 8 9 /** 10 * Lock a Surface that's been properly configured with {@link SurfaceHolder.setFixedSize()} and 11 * {@link SurfaceHolder.setFormat()}. The format must be YV12. 12 * 13 * @return A native handle to a native view of the locked surface. It needs to be closed with 14 * {@link close()}. 15 */ lockSurface(Surface surface)16 public static NativeSurfaceHandle lockSurface(Surface surface) { 17 long handle = AndroidBufferUtilities.lockSurface(surface); 18 if (handle == 0L) { 19 return null; 20 } 21 return new NativeSurfaceHandle(handle); 22 } NativeSurfaceHandle(long handle)23 public NativeSurfaceHandle(long handle) { 24 mHandle = handle; 25 } 26 handle()27 public long handle() { 28 return mHandle; 29 } 30 31 /** 32 * Obtain a HalideYuvBufferT from a handle to a locked surface. It needs to be deallocated 33 * with close(). 34 */ allocNativeYuvBufferT()35 public HalideYuvBufferT allocNativeYuvBufferT() { 36 if (mHandle == 0L){ 37 throw new IllegalStateException("Surface already unlocked."); 38 } 39 long yuvHandle = AndroidBufferUtilities.allocNativeYuvBufferTFromSurfaceHandle(mHandle); 40 return new HalideYuvBufferT(yuvHandle); 41 } 42 43 @Override close()44 public void close() { 45 if (mHandle != 0L) { 46 AndroidBufferUtilities.unlockSurface(mHandle); 47 mHandle = 0L; 48 } 49 } 50 51 @Override finalize()52 protected void finalize() { 53 close(); 54 } 55 } 56