content: /*
   * GPUPixel
   *
   * Created by PixPark on 2021/6/24.
   * Copyright © 2021 PixPark. All rights reserved.
   */
  
  package com.pixpark.gpupixel;
  
  import android.content.Context;
  import android.content.res.AssetManager;
  import android.graphics.Bitmap;
  import android.graphics.BitmapFactory;
  import android.opengl.GLES20;
  import android.opengl.GLUtils;
  import android.util.Log;
  
  import java.io.IOException;
  
  public class GPUPixelSourceImage extends GPUPixelSource {
      private static final String TAG = "GPUPixelSourceImage";
      protected  Bitmap bitmap;
      private GPUPixel.GPUPixelLandmarkCallback landmarkCallback;
      private final Object objectThis;
      public GPUPixelSourceImage(Bitmap bitmap) {
          objectThis = this;
          if (mNativeClassID != 0) return;
          GPUPixel.getInstance().runOnDraw(new Runnable() {
              @Override
              public void run() {
                  mNativeClassID = GPUPixel.nativeSourceImageNew();
              }
          });
          setImage(bitmap);
      }
  
      public void setImage(final Bitmap bitmap) {
          this.bitmap = bitmap;
          GPUPixel.getInstance().runOnDraw(new Runnable() {
              @Override
              public void run() {
                  if (mNativeClassID != 0)
                      GPUPixel.nativeSourceImageSetImage(mNativeClassID, bitmap);
              }
          });
      }
  
      public void destroy() {
          destroy(true);
      }
  
      public void destroy(boolean onGLThread) {
          if (mNativeClassID != 0) {
              if (onGLThread) {
                  GPUPixel.getInstance().runOnDraw(new Runnable() {
                      @Override
                      public void run() {
                          if (mNativeClassID != 0) {
                              GPUPixel.nativeSourceImageDestroy(mNativeClassID);
                              mNativeClassID = 0;
                          }
                      }
                  });
              } else {
                  GPUPixel.nativeSourceImageDestroy(mNativeClassID);
                  mNativeClassID = 0;
              }
          }
      }
  
      @Override
      protected void finalize() throws Throwable {
          try {
              if (mNativeClassID != 0) {
                  if (GPUPixel.getInstance().getGLSurfaceView() != null) {
                      GPUPixel.getInstance().runOnDraw(new Runnable() {
                          @Override
                          public void run() {
                              GPUPixel.nativeSourceImageFinalize(mNativeClassID);
                              mNativeClassID = 0;
                          }
                      });
                      GPUPixel.getInstance().requestRender();
                  } else {
                      GPUPixel.nativeSourceImageFinalize(mNativeClassID);
                      mNativeClassID = 0;
                  }
              }
          } finally {
              super.finalize();
          }
      }
  
      public static Bitmap createBitmap(Context context, String img_name) {
          Bitmap bitmap = null;
          try {
              bitmap = BitmapFactory.decodeStream(context.getAssets().open(img_name));
          } catch (IOException e) {
              e.printStackTrace();
          }
  
          return bitmap;
      }
  
      private int createTexture(Context context, String img_name) {
          try {
              bitmap = BitmapFactory.decodeStream(context.getAssets().open(img_name));
              int[] textureIds = new int[1];
              GLES20.glGenTextures(1, textureIds, 0);
              int error;
              while ((error = GLES20.glGetError()) != GLES20.GL_NO_ERROR) {
                  Log.e(TAG, "OpenGL error: " + error);
              }
              int textureId = textureIds[0];
  
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, textureId);
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MIN_FILTER, GLES20.GL_LINEAR);
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_MAG_FILTER, GLES20.GL_LINEAR);
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_S, GLES20.GL_CLAMP_TO_EDGE);
              GLES20.glTexParameteri(GLES20.GL_TEXTURE_2D, GLES20.GL_TEXTURE_WRAP_T, GLES20.GL_CLAMP_TO_EDGE);
  
              GLUtils.texImage2D(GLES20.GL_TEXTURE_2D, 0, bitmap, 0);
  
              GLES20.glBindTexture(GLES20.GL_TEXTURE_2D, 0);
              return textureId;
          } catch (IOException e) {
              e.printStackTrace();
              return -1;
          }
      }
  
      public void render() {
          GPUPixel.getInstance().runOnDraw(() -> {
              if (mNativeClassID != 0) {
                  GPUPixel.nativeSourceImageRender(mNativeClassID);
              }
          });
      }
  
      public void setLandmarkCallback(GPUPixel.GPUPixelLandmarkCallback callback) {
          landmarkCallback = callback;
          GPUPixel.getInstance().runOnDraw(() -> {
              if (mNativeClassID != 0) {
                  GPUPixel.nativeSetLandmarkCallback(objectThis, mNativeClassID);
              }
          });
      }
  
      // callback by native
      public void onFaceLandmark(float[] landmarks, float[] rect) {
          if (landmarkCallback != null) {
              landmarkCallback.onFaceLandmark(landmarks, rect);
          }
      }
  }
  