package com.bef.effectsdk;

import X.C05700Lw;
import android.opengl.GLDebugHelper;
import com.bytedance.covode.number.Covode;
import java.lang.ref.WeakReference;
import javax.microedition.khronos.egl.EGL10;
import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.egl.EGLContext;
import javax.microedition.khronos.egl.EGLDisplay;
import javax.microedition.khronos.egl.EGLSurface;
import javax.microedition.khronos.opengles.GL;

public class GLTextureView$EglHelper {
    public EGL10 mEgl;
    public EGLConfig mEglConfig;
    public EGLContext mEglContext;
    public EGLDisplay mEglDisplay;
    public EGLSurface mEglSurface;
    public WeakReference<C05700Lw> mGLTextureViewWeakRef;

    static {
        Covode.recordClassIndex(2792);
    }

    public static void logEglErrorAsWarning(String str, String str2, int i) {
    }

    public void destroySurface() {
        destroySurfaceImp();
    }

    public int swap() {
        if (!this.mEgl.eglSwapBuffers(this.mEglDisplay, this.mEglSurface)) {
            return this.mEgl.eglGetError();
        }
        return 12288;
    }

    private void destroySurfaceImp() {
        EGLSurface eGLSurface = this.mEglSurface;
        if (eGLSurface != null && eGLSurface != EGL10.EGL_NO_SURFACE) {
            EGL10 egl10 = this.mEgl;
            EGLDisplay eGLDisplay = this.mEglDisplay;
            EGLSurface eGLSurface2 = EGL10.EGL_NO_SURFACE;
            egl10.eglMakeCurrent(eGLDisplay, eGLSurface2, eGLSurface2, EGL10.EGL_NO_CONTEXT);
            C05700Lw r0 = (C05700Lw) this.mGLTextureViewWeakRef.get();
            if (r0 != null) {
                r0.mEGLWindowSurfaceFactory.destroySurface(this.mEgl, this.mEglDisplay, this.mEglSurface);
            }
            this.mEglSurface = null;
        }
    }

    public GL createGL() {
        GL gl = this.mEglContext.getGL();
        C05700Lw r4 = (C05700Lw) this.mGLTextureViewWeakRef.get();
        if (r4 == null) {
            return gl;
        }
        if (r4.mGLWrapper != null) {
            gl = r4.mGLWrapper.wrap(gl);
        }
        if ((r4.mDebugFlags & 3) == 0) {
            return gl;
        }
        int i = 0;
        GLTextureView$LogWriter gLTextureView$LogWriter = null;
        if ((r4.mDebugFlags & 1) != 0) {
            i = 1;
        }
        if ((r4.mDebugFlags & 2) != 0) {
            gLTextureView$LogWriter = new GLTextureView$LogWriter();
        }
        return GLDebugHelper.wrap(gl, i, gLTextureView$LogWriter);
    }

    public void finish() {
        if (this.mEglContext != null) {
            C05700Lw r0 = (C05700Lw) this.mGLTextureViewWeakRef.get();
            if (r0 != null) {
                r0.mEGLContextFactory.destroyContext(this.mEgl, this.mEglDisplay, this.mEglContext);
            }
            this.mEglContext = null;
        }
        EGLDisplay eGLDisplay = this.mEglDisplay;
        if (eGLDisplay != null) {
            this.mEgl.eglTerminate(eGLDisplay);
            this.mEglDisplay = null;
        }
    }

    public boolean createSurface() {
        if (this.mEgl == null) {
            throw new RuntimeException("egl not initialized");
        } else if (this.mEglDisplay == null) {
            throw new RuntimeException("eglDisplay not initialized");
        } else if (this.mEglConfig != null) {
            destroySurfaceImp();
            C05700Lw r0 = (C05700Lw) this.mGLTextureViewWeakRef.get();
            if (r0 != null) {
                this.mEglSurface = r0.mEGLWindowSurfaceFactory.createWindowSurface(this.mEgl, this.mEglDisplay, this.mEglConfig, r0.getSurfaceTexture());
            } else {
                this.mEglSurface = null;
            }
            EGLSurface eGLSurface = this.mEglSurface;
            if (eGLSurface == null || eGLSurface == EGL10.EGL_NO_SURFACE) {
                this.mEgl.eglGetError();
                return false;
            }
            EGL10 egl10 = this.mEgl;
            EGLDisplay eGLDisplay = this.mEglDisplay;
            EGLSurface eGLSurface2 = this.mEglSurface;
            if (egl10.eglMakeCurrent(eGLDisplay, eGLSurface2, eGLSurface2, this.mEglContext)) {
                return true;
            }
            logEglErrorAsWarning("EGLHelper", "eglMakeCurrent", this.mEgl.eglGetError());
            return false;
        } else {
            throw new RuntimeException("mEglConfig not initialized");
        }
    }

    public void start() {
        EGL10 egl10 = (EGL10) EGLContext.getEGL();
        this.mEgl = egl10;
        EGLDisplay eglGetDisplay = egl10.eglGetDisplay(EGL10.EGL_DEFAULT_DISPLAY);
        this.mEglDisplay = eglGetDisplay;
        if (eglGetDisplay != EGL10.EGL_NO_DISPLAY) {
            if (this.mEgl.eglInitialize(this.mEglDisplay, new int[2])) {
                C05700Lw r3 = (C05700Lw) this.mGLTextureViewWeakRef.get();
                if (r3 == null) {
                    this.mEglConfig = null;
                    this.mEglContext = null;
                } else {
                    this.mEglConfig = r3.mEGLConfigChooser.chooseConfig(this.mEgl, this.mEglDisplay);
                    this.mEglContext = r3.mEGLContextFactory.createContext(this.mEgl, this.mEglDisplay, this.mEglConfig);
                }
                EGLContext eGLContext = this.mEglContext;
                if (eGLContext == null || eGLContext == EGL10.EGL_NO_CONTEXT) {
                    this.mEglContext = null;
                    throwEglException("createContext");
                }
                this.mEglSurface = null;
                return;
            }
            throw new RuntimeException("eglInitialize failed");
        }
        throw new RuntimeException("eglGetDisplay failed");
    }

    public GLTextureView$EglHelper(WeakReference<C05700Lw> weakReference) {
        this.mGLTextureViewWeakRef = weakReference;
    }

    private void throwEglException(String str) {
        throwEglException(str, this.mEgl.eglGetError());
    }

    public static String formatEglError(String str, int i) {
        return str + " failed";
    }

    public static void throwEglException(String str, int i) {
        throw new RuntimeException(formatEglError(str, i));
    }
}
