public SurfaceEncoder(){ super.setOnDrawEndListener(new OnDrawEndListener() { @Override public void onDrawEnd(EGLSurface surface, RenderBean bean) { AvLog.d(TAG,"onDrawEnd start-->"); if(bean.timeStamp!=-1){ bean.egl.setPresentationTime(surface,bean.timeStamp*1000); }else{ if(startTime==-1){ startTime=bean.textureTime; } bean.egl.setPresentationTime(surface,bean.textureTime-startTime); } videoEncodeStep(false); AvLog.e(TAG,"onDrawEnd end-->"); if(mListener!=null){ mListener.onDrawEnd(surface,bean); } } }); }
public EGLSurface createGLESWithPBuffer(EGLConfigAttrs attrs,EGLContextAttrs ctxAttrs,int width,int height){ EGLConfig config=getConfig(attrs.surfaceType(EGL14.EGL_PBUFFER_BIT)); if(config==null){ log("getConfig failed : "+EGL14.eglGetError()); return null; } EGLContext eglContext=createContext(config,EGL14.EGL_NO_CONTEXT,ctxAttrs); if(eglContext==EGL14.EGL_NO_CONTEXT){ log("createContext failed : "+EGL14.eglGetError()); return null; } EGLSurface eglSurface=createPBufferSurface(config,width,height); if(eglSurface==EGL14.EGL_NO_SURFACE){ log("createWindowSurface failed : "+EGL14.eglGetError()); return null; } if(!EGL14.eglMakeCurrent(mEGLDisplay,eglSurface,eglSurface,eglContext)){ log("eglMakeCurrent failed : "+EGL14.eglGetError()); return null; } return eglSurface; }
/** * change context to draw this window surface * @return */ private boolean makeCurrent(final EGLSurface surface) { // if (DEBUG) Log.v(TAG, "makeCurrent:"); if (mEglDisplay == null) { if (DEBUG) Log.d(TAG, "makeCurrent:eglDisplay not initialized"); } if (surface == null || surface == EGL14.EGL_NO_SURFACE) { final int error = EGL14.eglGetError(); if (error == EGL14.EGL_BAD_NATIVE_WINDOW) { Log.e(TAG, "makeCurrent:returned EGL_BAD_NATIVE_WINDOW."); } return false; } // attach EGL renderring context to specific EGL window surface if (!EGL14.eglMakeCurrent(mEglDisplay, surface, surface, mEglContext)) { Log.w(TAG, "eglMakeCurrent:" + EGL14.eglGetError()); return false; } return true; }
/** * Test for {@link GLTools#setPresentationTime(EGLDisplay, EGLSurface, long)} . * @throws Exception by some fails */ @Test public final void testSetPresentationTime() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); final int txt = GLTools.newTexture(TEXTURE_LEVEL); final SurfaceTexture surfaceTexture = new SurfaceTexture(txt, true); final Surface surface = new Surface(surfaceTexture); final EGLSurface window = GLTools.newSurface(eglDisplay, eglConfig, surface); GLTools.setPresentationTime(eglDisplay, window, PRESENTATION_TIME); GLTools.closeSurface(eglDisplay, window); surface.release(); surfaceTexture.release(); GLTools.closeTexture(txt, TEXTURE_LEVEL); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newTexture(int)} and {@link GLTools#closeTexture(int, int)}. * @throws Exception by some fails */ @Test public final void testTexture() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); GLTools.closeTexture(GLTools.newTexture(TEXTURE_LEVEL), TEXTURE_LEVEL); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#newShader(int[])} and {@link GLTools#closeShader(int[])}. * @throws Exception by some fails */ @Test public final void testShader() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); final int[] attrs = new int[5]; GLTools.newShader(attrs); GLTools.closeShader(attrs); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Creates an EGL surface associated with a Surface. * <p> * If this is destined for MediaCodec, the EGLConfig should have the "recordable" attribute. */ public EGLSurface createWindowSurface(Object surface) { if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) { throw new RuntimeException("invalid surface: " + surface); } // Create a window surface, and attach it to the Surface we received. int[] surfaceAttribs = { EGL14.EGL_NONE }; EGLSurface eglSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface, surfaceAttribs, 0); checkEglError("eglCreateWindowSurface"); if (eglSurface == null) { throw new RuntimeException("surface was null"); } return eglSurface; }
public EGLSurface createEGLSurface(Object surface) { if (!(surface instanceof Surface) && !(surface instanceof SurfaceTexture)) { throw new RuntimeException("invalid surface: " + surface); } // Create a window surface, and attach it to the Surface we received. int[] surfaceAttributes = { EGL14.EGL_NONE }; EGLSurface eglSurface = EGL14.eglCreateWindowSurface(mEGLDisplay, mEGLConfig, surface, surfaceAttributes, 0); if (eglSurface == null) { throw new RuntimeException("surface was null"); } return eglSurface; }
private void cameraInit(){ mShower=new SurfaceShower(); mProvider=new CameraProvider(); mProcessor=new VideoSurfaceProcessor(); mProcessor.setTextureProvider(mProvider); mProcessor.addObserver(mShower); mFb=new FrameBuffer(); mShower.setOnDrawEndListener(new SurfaceShower.OnDrawEndListener() { @Override public void onDrawEnd(EGLSurface surface, RenderBean bean) { if(exportFlag){ if(mOutputFilter==null){ mOutputFilter=new YuvOutputFilter(YuvOutputFilter.EXPORT_TYPE_NV21); mOutputFilter.create(); mOutputFilter.sizeChanged(picX,picY); mOutputFilter.setInputTextureSize(bean.sourceWidth,bean.sourceHeight); tempBuffer=new byte[picX*picY*3/2]; } mOutputFilter.drawToTexture(bean.textureId); mOutputFilter.getOutput(tempBuffer,0,picX*picY*3/2); runOnUiThread(new Runnable() { @Override public void run() { if(mBitmap!=null){ mBitmap.recycle(); mBitmap=null; } mBitmap=rawByteArray2RGBABitmap2(tempBuffer,picX,picY); mImage.setImageBitmap(mBitmap); mImage.setVisibility(View.VISIBLE); } }); exportFlag=false; } } }); }
/** * Writes the current display, context, and surface to the log. */ public static void logCurrent(String msg) { EGLDisplay display; EGLContext context; EGLSurface surface; display = EGL14.eglGetCurrentDisplay(); context = EGL14.eglGetCurrentContext(); surface = EGL14.eglGetCurrentSurface(EGL14.EGL_DRAW); Log.i(TAG, "Current EGL (" + msg + "): display=" + display + ", context=" + context + ", surface=" + surface); }
public boolean destroyGLES(EGLSurface surface,EGLContext context){ EGL14.eglMakeCurrent(mEGLDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT); if(surface!=null){ EGL14.eglDestroySurface(mEGLDisplay,surface); } if(context!=null){ EGL14.eglDestroyContext(mEGLDisplay,context); } EGL14.eglTerminate(mEGLDisplay); log("gl destroy gles"); return true; }
private int swap(final EGLSurface surface) { // if (DEBUG) Log.v(TAG, "swap:"); if (!EGL14.eglSwapBuffers(mEglDisplay, surface)) { final int err = EGL14.eglGetError(); if (DEBUG) Log.w(TAG, "swap:err=" + err); return err; } return EGL14.EGL_SUCCESS; }
private EGLSurface createWindowSurface(final Object nativeWindow) { if (DEBUG) Log.v(TAG, "createWindowSurface:nativeWindow=" + nativeWindow); final int[] surfaceAttribs = { EGL14.EGL_NONE }; EGLSurface result = null; try { result = EGL14.eglCreateWindowSurface(mEglDisplay, mEglConfig, nativeWindow, surfaceAttribs, 0); } catch (final IllegalArgumentException e) { Log.e(TAG, "eglCreateWindowSurface", e); } return result; }
private void destroyWindowSurface(EGLSurface surface) { if (DEBUG) Log.v(TAG, "destroySurface:"); if (surface != EGL14.EGL_NO_SURFACE) { EGL14.eglMakeCurrent(mEglDisplay, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_SURFACE, EGL14.EGL_NO_CONTEXT); EGL14.eglDestroySurface(mEglDisplay, surface); } surface = EGL14.EGL_NO_SURFACE; if (DEBUG) Log.v(TAG, "destroySurface:finished"); }
/** * Creates an EGL surface associated with an offscreen buffer. */ public EGLSurface createOffscreenSurface(int width, int height) { int[] surfaceAttribs = { EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE }; EGLSurface eglSurface = EGL14.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceAttribs, 0); checkEglError("eglCreatePbufferSurface"); if (eglSurface == null) { throw new RuntimeException("surface was null"); } return eglSurface; }
/** * Makes our EGL context current, using the supplied surface for both "draw" and "read". */ public void makeCurrent(EGLSurface eglSurface) { if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { // called makeCurrent() before create? Log.d(TAG, "NOTE: makeCurrent w/o display"); } if (!EGL14.eglMakeCurrent(mEGLDisplay, eglSurface, eglSurface, mEGLContext)) { throw new RuntimeException("eglMakeCurrent failed"); } }
/** * Makes our EGL context current, using the supplied "draw" and "read" surfaces. */ public void makeCurrent(EGLSurface drawSurface, EGLSurface readSurface) { if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { // called makeCurrent() before create? Log.d(TAG, "NOTE: makeCurrent w/o display"); } if (!EGL14.eglMakeCurrent(mEGLDisplay, drawSurface, readSurface, mEGLContext)) { throw new RuntimeException("eglMakeCurrent(draw,read) failed"); } }
/** * @param display an EGL display connection instance * @param width horizontal size of virtual surface * @param height vertical size of virtual surface * @param config a frame buffer configuration * @return a new EGL window surface **/ @NonNull public static EGLSurface newSurface(@NonNull EGLDisplay display, @NonNull EGLConfig config, int width, int height) { final EGLSurface result = EGL14.eglCreatePbufferSurface(display, config, new int[] {EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE}, 0); if (result == EGL14.EGL_NO_SURFACE) { logError(); throw new RuntimeException("Unable to from EGL14 context"); } else { logDebug(getSurfaceString(result) + " created"); return result; } }
/** * @param display an EGL display connection instance * @param surface a native window. * @param config a frame buffer configuration * @return a new EGL window surface **/ @NonNull public static EGLSurface newSurface(@NonNull EGLDisplay display, @NonNull EGLConfig config, @NonNull Surface surface) { final EGLSurface result = EGL14.eglCreateWindowSurface(display, config, surface, new int[] {EGL14.EGL_NONE}, 0); if (result == EGL14.EGL_NO_SURFACE) { logError(); throw new RuntimeException("Unable to from EGL14 context"); } else { logDebug(getSurfaceString(result) + " created"); return result; } }
/** * Close an EGL rendering context. * @param display an EGL display connection instance * @param surface an EGL rendering surface */ public static void closeSurface(@NonNull EGLDisplay display, @NonNull EGLSurface surface) { if (!EGL14.eglDestroySurface(display, surface)) { logError(); throw new RuntimeException("Unable to terminate EGL14 " + getSurfaceString(surface)); } else { logDebug(getSurfaceString(surface) + " destroyed"); } }
/** * Make an EGL rendering context as current. * @param display an EGL display connection instance * @param surface an EGL rendering surface * @param context an EGL rendering context */ public static void makeCurrent(@NonNull EGLDisplay display, @NonNull EGLSurface surface, @NonNull EGLContext context) { if (!EGL14.eglMakeCurrent(display, surface, surface, context)) { logError(); throw new RuntimeException("Unable to make " + getContextString(context) + " current"); } else { logDebug(getContextString(context) + " set as current"); } }
/** * Calls eglSwapBuffers. Use this to "publish" the current frame. * @param display an EGL display connection instance * @param surface an EGL rendering surface */ public static void swapBuffers(@NonNull EGLDisplay display, @NonNull EGLSurface surface) { //noinspection StatementWithEmptyBody if (!EGL14.eglSwapBuffers(display, surface)) { logError(); throw new RuntimeException("Unable to swap buffers. " + getDisplayString(display) + "; " + getSurfaceString(surface)); } else {/* logDebug("Swap buffers. " + getDisplayString(display) + "; " + getSurfaceString(surface));*/ } }
/** * Sends the presentation time stamp to EGL. * @param display an EGL display connection instance * @param surface an EGL rendering surface * @param nSecs time is expressed in nanoseconds. */ public static void setPresentationTime(@NonNull EGLDisplay display, @NonNull EGLSurface surface, long nSecs) { //noinspection StatementWithEmptyBody if (!EGLExt.eglPresentationTimeANDROID(display, surface, nSecs)) { logError(); throw new RuntimeException("Unable to set presentation time (" + nSecs +"ns)." + getDisplayString(display) + "; " + getSurfaceString(surface)); } else {/* logDebug("Set presentation time (" + nSecs +"ns)." + getDisplayString(display) + "; " + getSurfaceString(surface));*/ } }
/** * Test for {@link GLTools#makeCurrent(EGLDisplay, EGLSurface, EGLContext)}. * @throws Exception by some fails */ @Test public final void testMakeCurrent() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
/** * Test for {@link GLTools#swapBuffers(EGLDisplay, EGLSurface)}. * @throws Exception by some fails */ @Test public final void testSwapBuffers() throws Exception { final EGLDisplay eglDisplay = GLTools.newDisplay(); final EGLConfig eglConfig = GLTools.newConfig(eglDisplay, true); final EGLContext eglContext = GLTools.newContext(eglDisplay, eglConfig); final EGLSurface eglSurface = GLTools.newSurface(eglDisplay, eglConfig, FRAME_SIZE, FRAME_SIZE); GLTools.makeCurrent(eglDisplay, eglSurface, eglContext); try { GLTools.swapBuffers(eglDisplay, eglSurface); } catch (RuntimeException exception) { final int txt = GLTools.newTexture(TEXTURE_LEVEL); final SurfaceTexture surfaceTexture = new SurfaceTexture(txt, true); final Surface surface = new Surface(surfaceTexture); final EGLSurface window = GLTools.newSurface(eglDisplay, eglConfig, surface); GLTools.makeCurrent(eglDisplay, window, eglContext); GLTools.swapBuffers(eglDisplay, window); GLTools.closeSurface(eglDisplay, window); surface.release(); surfaceTexture.release(); GLTools.closeTexture(txt, TEXTURE_LEVEL); } GLTools.closeSurface(eglDisplay, eglSurface); GLTools.closeContext(eglDisplay, eglContext); GLTools.closeDisplay(eglDisplay); }
public EGLSurface createEGLSurface(int width, int height) { int[] surfaceAttributes = { EGL14.EGL_WIDTH, width, EGL14.EGL_HEIGHT, height, EGL14.EGL_NONE }; EGLSurface eglSurface = EGL14.eglCreatePbufferSurface(mEGLDisplay, mEGLConfig, surfaceAttributes, 0); if (eglSurface == null) { throw new RuntimeException("surface was null"); } return eglSurface; }
public void makeCurrent(EGLSurface drawSurface, EGLSurface readSurface) { if (mEGLDisplay == EGL14.EGL_NO_DISPLAY) { // called makeCurrent() before create? Log.d(TAG, "NOTE: makeCurrent w/o display"); } if (!EGL14.eglMakeCurrent(mEGLDisplay, drawSurface, readSurface, mEGLContext)) { throw new RuntimeException("eglMakeCurrent(draw,read) failed"); } }