package com.bytedance.common.graphics;

import X.AnonymousClass9OI;
import X.C25148e1;
import X.C25158e2;
import X.C25298eG;
import X.C25348eL;
import X.C44549Mf;
import X.C45659Qm;
import X.C46399Ti;
import X.C46699Um;
import android.content.Context;
import com.bytedance.covode.number.Covode;
import com.bytedance.frameworks.apm.trace.MethodCollector;
import java.util.concurrent.ScheduledExecutorService;
import java.util.concurrent.ScheduledFuture;
import java.util.concurrent.TimeUnit;

public class GraphicsMonitor {
    public static ScheduledFuture<?> gpuFuture;
    public static double gpuLoadDataOnce = -1.0d;
    public static ScheduledExecutorService gpuScheduleService;
    public static boolean isInit;
    public static boolean isInitGraphicsLoad;
    public static volatile boolean isPause;
    public static C45659Qm lifecycleService;
    public static long sCollectInterval;
    public static long sCollectWindow;
    public static int startTime;

    public static native void closeStatistical();

    public static native double getStatisticOnceData();

    public static native void openStatistical();

    public static native boolean startHook();

    static {
        Covode.recordClassIndex(24679);
    }

    public static synchronized double getByteGpu() {
        double d;
        synchronized (GraphicsMonitor.class) {
            MethodCollector.i(18071);
            d = gpuLoadDataOnce;
            MethodCollector.o(18071);
        }
        return d;
    }

    public static synchronized boolean isStart() {
        synchronized (GraphicsMonitor.class) {
            MethodCollector.i(18075);
            if (startTime > 0) {
                MethodCollector.o(18075);
                return true;
            }
            MethodCollector.o(18075);
            return false;
        }
    }

    public static void initGraphicsLoad() {
        MethodCollector.i(12050);
        isInitGraphicsLoad = true;
        try {
            C44549Mf.LIZ((Context) AnonymousClass9OI.LJII);
            startHook();
            gpuScheduleService = INVOKESTATIC_com_bytedance_common_graphics_GraphicsMonitor_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newScheduledThreadPool(0);
            MethodCollector.o(12050);
        } catch (Throwable unused) {
            isInitGraphicsLoad = false;
            MethodCollector.o(12050);
        }
    }

    public static synchronized void stop() {
        synchronized (GraphicsMonitor.class) {
            MethodCollector.i(11846);
            if (!isInit || !isStart()) {
                MethodCollector.o(11846);
                return;
            }
            int i = startTime - 1;
            startTime = i;
            if (i > 0) {
                MethodCollector.o(11846);
                return;
            }
            ScheduledFuture<?> scheduledFuture = gpuFuture;
            if (scheduledFuture != null) {
                scheduledFuture.cancel(true);
            }
            gpuLoadDataOnce = -1.0d;
            MethodCollector.o(11846);
        }
    }

    public static synchronized void init() {
        synchronized (GraphicsMonitor.class) {
            MethodCollector.i(18595);
            if (isInit) {
                MethodCollector.o(18595);
                return;
            }
            isInit = true;
            sCollectInterval = 15000;
            sCollectWindow = 100;
            C45659Qm r1 = (C45659Qm) C46399Ti.LIZ(C45659Qm.class);
            lifecycleService = r1;
            r1.LIZ(new C46699Um() {
                static {
                    Covode.recordClassIndex(24680);
                }

                public final void LIZ() {
                    GraphicsMonitor.isPause = false;
                }

                public final void LIZIZ() {
                    GraphicsMonitor.isPause = true;
                }
            });
            if (lifecycleService.LIZ()) {
                isPause = false;
            }
            MethodCollector.o(18595);
        }
    }

    public static synchronized void start() {
        synchronized (GraphicsMonitor.class) {
            MethodCollector.i(18632);
            if (!isInit) {
                MethodCollector.o(18632);
                return;
            }
            if (!isInitGraphicsLoad) {
                initGraphicsLoad();
            }
            ScheduledExecutorService scheduledExecutorService = gpuScheduleService;
            if (scheduledExecutorService == null) {
                MethodCollector.o(18632);
                return;
            }
            int i = startTime + 1;
            startTime = i;
            if (i > 1) {
                MethodCollector.o(18632);
                return;
            }
            AnonymousClass2 r5 = new Runnable() {
                static {
                    Covode.recordClassIndex(24681);
                }

                public final void run() {
                    MethodCollector.i(11610);
                    try {
                        if (!GraphicsMonitor.isPause) {
                            GraphicsMonitor.openStatistical();
                            Thread.sleep(GraphicsMonitor.sCollectWindow);
                            GraphicsMonitor.closeStatistical();
                            GraphicsMonitor.gpuLoadDataOnce = GraphicsMonitor.getStatisticOnceData();
                            MethodCollector.o(11610);
                            return;
                        }
                        GraphicsMonitor.gpuLoadDataOnce = -1.0d;
                        MethodCollector.o(11610);
                    } catch (Throwable unused) {
                        MethodCollector.o(11610);
                    }
                }
            };
            long j = sCollectInterval;
            gpuFuture = scheduledExecutorService.scheduleWithFixedDelay(r5, j, j, TimeUnit.MILLISECONDS);
            MethodCollector.o(18632);
        }
    }

    public static ScheduledExecutorService INVOKESTATIC_com_bytedance_common_graphics_GraphicsMonitor_com_ss_android_ugc_aweme_lancet_thread_ThreadPoolLancet_newScheduledThreadPool(int i) {
        C25148e1 LIZ = C25158e2.LIZ(C25298eG.SCHEDULED);
        LIZ.LIZJ = i;
        return (ScheduledExecutorService) C25348eL.LIZ(LIZ.LIZ());
    }
}
