package p138.p176.p177.p190.p240;

import android.annotation.TargetApi;
import android.content.Context;
import android.graphics.SurfaceTexture;
import android.opengl.EGL14;
import android.opengl.EGLConfig;
import android.opengl.EGLContext;
import android.opengl.EGLDisplay;
import android.opengl.EGLSurface;
import android.opengl.GLES20;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Message;
import android.view.Surface;
import com.google.android.exoplayer2.util.EGLSurfaceTexture;
import p000.p001.p002.p003.p004.C0567;
import p138.p176.p177.p190.p239.C4131;
import p138.p176.p177.p190.p239.C4150;

/* JADX INFO: renamed from: ދ.ހ.֏.ؠ.ဨ.ވ, reason: contains not printable characters */
/* JADX INFO: loaded from: classes.dex */
@TargetApi(17)
public final class C4162 extends Surface {

    /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
    public static int f13303;

    /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
    public static boolean f13304;

    /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
    public final HandlerThreadC4164 f13305;

    /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
    public boolean f13306;

    /* JADX INFO: renamed from: ދ.ހ.֏.ؠ.ဨ.ވ$ؠ, reason: contains not printable characters */
    public static class HandlerThreadC4164 extends HandlerThread implements Handler.Callback {

        /* JADX INFO: renamed from: ރ, reason: contains not printable characters */
        public EGLSurfaceTexture f13307;

        /* JADX INFO: renamed from: ބ, reason: contains not printable characters */
        public Handler f13308;

        /* JADX INFO: renamed from: ޅ, reason: contains not printable characters */
        public Error f13309;

        /* JADX INFO: renamed from: ކ, reason: contains not printable characters */
        public RuntimeException f13310;

        /* JADX INFO: renamed from: އ, reason: contains not printable characters */
        public C4162 f13311;

        public HandlerThreadC4164() {
            super("dummySurface");
        }

        @Override // android.os.Handler.Callback
        public boolean handleMessage(Message message) {
            int i = message.what;
            try {
                if (i != 1) {
                    if (i != 2) {
                        return true;
                    }
                    try {
                        m6817();
                    } finally {
                        try {
                        } finally {
                        }
                    }
                    return true;
                }
                try {
                    m6818(message.arg1);
                    synchronized (this) {
                        notify();
                    }
                } catch (Error e) {
                    C4131.m6645("DummySurface", "Failed to initialize dummy surface", e);
                    this.f13309 = e;
                    synchronized (this) {
                        notify();
                    }
                } catch (RuntimeException e2) {
                    C4131.m6645("DummySurface", "Failed to initialize dummy surface", e2);
                    this.f13310 = e2;
                    synchronized (this) {
                        notify();
                    }
                }
                return true;
            } catch (Throwable th) {
                synchronized (this) {
                    notify();
                    throw th;
                }
            }
        }

        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public C4162 m6816(int i) {
            boolean z;
            start();
            Handler handler = new Handler(getLooper(), this);
            this.f13308 = handler;
            this.f13307 = new EGLSurfaceTexture(handler);
            synchronized (this) {
                z = false;
                this.f13308.obtainMessage(1, i, 0).sendToTarget();
                while (this.f13311 == null && this.f13310 == null && this.f13309 == null) {
                    try {
                        wait();
                    } catch (InterruptedException unused) {
                        z = true;
                    }
                }
            }
            if (z) {
                Thread.currentThread().interrupt();
            }
            RuntimeException runtimeException = this.f13310;
            if (runtimeException != null) {
                throw runtimeException;
            }
            Error error = this.f13309;
            if (error != null) {
                throw error;
            }
            C4162 c4162 = this.f13311;
            C0567.m1856(c4162);
            return c4162;
        }

        /* JADX WARN: Multi-variable type inference failed */
        /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
        public final void m6817() {
            C0567.m1856(this.f13307);
            EGLSurfaceTexture eGLSurfaceTexture = this.f13307;
            eGLSurfaceTexture.f2015.removeCallbacks(eGLSurfaceTexture);
            try {
                if (eGLSurfaceTexture.f2021 != null) {
                    eGLSurfaceTexture.f2021.release();
                    GLES20.glDeleteTextures(1, eGLSurfaceTexture.f2016, 0);
                }
            } finally {
                EGLDisplay eGLDisplay = eGLSurfaceTexture.f2018;
                if (eGLDisplay != null && !eGLDisplay.equals(EGL14.EGL_NO_DISPLAY)) {
                    EGLDisplay eGLDisplay2 = eGLSurfaceTexture.f2018;
                    EGLSurface eGLSurface = EGL14.EGL_NO_SURFACE;
                    EGL14.eglMakeCurrent(eGLDisplay2, eGLSurface, eGLSurface, EGL14.EGL_NO_CONTEXT);
                }
                EGLSurface eGLSurface2 = eGLSurfaceTexture.f2020;
                if (eGLSurface2 != null && !eGLSurface2.equals(EGL14.EGL_NO_SURFACE)) {
                    EGL14.eglDestroySurface(eGLSurfaceTexture.f2018, eGLSurfaceTexture.f2020);
                }
                EGLContext eGLContext = eGLSurfaceTexture.f2019;
                if (eGLContext != null) {
                    EGL14.eglDestroyContext(eGLSurfaceTexture.f2018, eGLContext);
                }
                if (C4150.f13259 >= 19) {
                    EGL14.eglReleaseThread();
                }
                EGLDisplay eGLDisplay3 = eGLSurfaceTexture.f2018;
                if (eGLDisplay3 != null && !eGLDisplay3.equals(EGL14.EGL_NO_DISPLAY)) {
                    EGL14.eglTerminate(eGLSurfaceTexture.f2018);
                }
                eGLSurfaceTexture.f2018 = null;
                eGLSurfaceTexture.f2019 = null;
                eGLSurfaceTexture.f2020 = null;
                eGLSurfaceTexture.f2021 = null;
            }
        }

        /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
        public final void m6818(int i) {
            EGLSurface eGLSurfaceEglCreatePbufferSurface;
            C0567.m1856(this.f13307);
            EGLSurfaceTexture eGLSurfaceTexture = this.f13307;
            C4163 c4163 = null;
            if (eGLSurfaceTexture == null) {
                throw null;
            }
            EGLDisplay eGLDisplayEglGetDisplay = EGL14.eglGetDisplay(0);
            if (eGLDisplayEglGetDisplay == null) {
                throw new EGLSurfaceTexture.GlException("eglGetDisplay failed", null);
            }
            int[] iArr = new int[2];
            if (!EGL14.eglInitialize(eGLDisplayEglGetDisplay, iArr, 0, iArr, 1)) {
                throw new EGLSurfaceTexture.GlException("eglInitialize failed", null);
            }
            eGLSurfaceTexture.f2018 = eGLDisplayEglGetDisplay;
            EGLConfig[] eGLConfigArr = new EGLConfig[1];
            int[] iArr2 = new int[1];
            boolean zEglChooseConfig = EGL14.eglChooseConfig(eGLDisplayEglGetDisplay, EGLSurfaceTexture.f2014, 0, eGLConfigArr, 0, 1, iArr2, 0);
            if (!zEglChooseConfig || iArr2[0] <= 0 || eGLConfigArr[0] == null) {
                throw new EGLSurfaceTexture.GlException(C4150.m6758("eglChooseConfig failed: success=%b, numConfigs[0]=%d, configs[0]=%s", Boolean.valueOf(zEglChooseConfig), Integer.valueOf(iArr2[0]), eGLConfigArr[0]), null);
            }
            EGLConfig eGLConfig = eGLConfigArr[0];
            EGLContext eGLContextEglCreateContext = EGL14.eglCreateContext(eGLSurfaceTexture.f2018, eGLConfig, EGL14.EGL_NO_CONTEXT, i == 0 ? new int[]{12440, 2, 12344} : new int[]{12440, 2, 12992, 1, 12344}, 0);
            if (eGLContextEglCreateContext == null) {
                throw new EGLSurfaceTexture.GlException("eglCreateContext failed", null);
            }
            eGLSurfaceTexture.f2019 = eGLContextEglCreateContext;
            EGLDisplay eGLDisplay = eGLSurfaceTexture.f2018;
            if (i == 1) {
                eGLSurfaceEglCreatePbufferSurface = EGL14.EGL_NO_SURFACE;
            } else {
                eGLSurfaceEglCreatePbufferSurface = EGL14.eglCreatePbufferSurface(eGLDisplay, eGLConfig, i == 2 ? new int[]{12375, 1, 12374, 1, 12992, 1, 12344} : new int[]{12375, 1, 12374, 1, 12344}, 0);
                if (eGLSurfaceEglCreatePbufferSurface == null) {
                    throw new EGLSurfaceTexture.GlException("eglCreatePbufferSurface failed", null);
                }
            }
            if (!EGL14.eglMakeCurrent(eGLDisplay, eGLSurfaceEglCreatePbufferSurface, eGLSurfaceEglCreatePbufferSurface, eGLContextEglCreateContext)) {
                throw new EGLSurfaceTexture.GlException("eglMakeCurrent failed", null);
            }
            eGLSurfaceTexture.f2020 = eGLSurfaceEglCreatePbufferSurface;
            GLES20.glGenTextures(1, eGLSurfaceTexture.f2016, 0);
            C0567.m1873();
            SurfaceTexture surfaceTexture = new SurfaceTexture(eGLSurfaceTexture.f2016[0]);
            eGLSurfaceTexture.f2021 = surfaceTexture;
            surfaceTexture.setOnFrameAvailableListener(eGLSurfaceTexture);
            SurfaceTexture surfaceTexture2 = this.f13307.f2021;
            C0567.m1856(surfaceTexture2);
            this.f13311 = new C4162(this, surfaceTexture2, i != 0, c4163);
        }
    }

    public /* synthetic */ C4162(HandlerThreadC4164 handlerThreadC4164, SurfaceTexture surfaceTexture, boolean z, C4163 c4163) {
        super(surfaceTexture);
        this.f13305 = handlerThreadC4164;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static int m6813(Context context) {
        String strEglQueryString;
        String strEglQueryString2;
        int i = C4150.f13259;
        boolean z = false;
        if (!(i >= 24 && (i >= 26 || !("samsung".equals(C4150.f13261) || "XT1650".equals(C4150.f13262))) && ((C4150.f13259 >= 26 || context.getPackageManager().hasSystemFeature("android.hardware.vr.high_performance")) && (strEglQueryString2 = EGL14.eglQueryString(EGL14.eglGetDisplay(0), 12373)) != null && strEglQueryString2.contains("EGL_EXT_protected_content")))) {
            return 0;
        }
        if (C4150.f13259 >= 17 && (strEglQueryString = EGL14.eglQueryString(EGL14.eglGetDisplay(0), 12373)) != null && strEglQueryString.contains("EGL_KHR_surfaceless_context")) {
            z = true;
        }
        return z ? 1 : 2;
    }

    /* JADX INFO: renamed from: ֏, reason: contains not printable characters */
    public static C4162 m6814(Context context, boolean z) {
        if (C4150.f13259 < 17) {
            throw new UnsupportedOperationException("Unsupported prior to API level 17");
        }
        C0567.m1932(!z || m6815(context));
        return new HandlerThreadC4164().m6816(z ? f13303 : 0);
    }

    /* JADX INFO: renamed from: ؠ, reason: contains not printable characters */
    public static synchronized boolean m6815(Context context) {
        if (!f13304) {
            f13303 = m6813(context);
            f13304 = true;
        }
        return f13303 != 0;
    }

    @Override // android.view.Surface
    public void release() {
        super.release();
        synchronized (this.f13305) {
            if (!this.f13306) {
                HandlerThreadC4164 handlerThreadC4164 = this.f13305;
                C0567.m1856(handlerThreadC4164.f13308);
                handlerThreadC4164.f13308.sendEmptyMessage(2);
                this.f13306 = true;
            }
        }
    }
}
