Skip to content

Commit

Permalink
update .
Browse files Browse the repository at this point in the history
  • Loading branch information
lambiengcode committed May 28, 2024
1 parent c37f5e9 commit 0a6c06e
Show file tree
Hide file tree
Showing 5 changed files with 74 additions and 18 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,6 @@
import android.util.Log;
import android.view.SurfaceHolder;
import android.view.WindowManager;
import android.widget.SeekBar;
import android.widget.Toast;

import com.pixpark.GPUPixelApp.databinding.ActivityMainBinding;
Expand Down Expand Up @@ -168,7 +167,7 @@ public void startCameraFilter() {
sourceCamera.addTarget(lipstickFilter);
lipstickFilter.addTarget(faceReshapFilter);
faceReshapFilter.addTarget(beautyFaceFilter);
beautyFaceFilter.addTargetCallback();
// beautyFaceFilter.addTargetCallback();

sourceCamera.setLandmarkCallbck(new GPUPixel.GPUPixelLandmarkCallback() {
@Override
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,11 @@ public class GPUPixel {
public interface GPUPixelLandmarkCallback {
public void onFaceLandmark(float[] landmarks);
}

public interface RawOutputCallback {
public void onRawOutput(byte[] data, int width, int height, int ts);
}

public static final int NoRotation = 0;
public static final int RotateLeft = 1;
public static final int RotateRight = 2;
Expand Down Expand Up @@ -227,7 +232,7 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
public static native void nativeSourceCameraDestroy(final long classID);
public static native void nativeSourceCameraFinalize(final long classID);
public static native void nativeSourceCameraSetFrame(final long classID, final int width, final int height, final int[] data, final int rotation);

public static native void nativeSourceCameraSetFrameByBuffer(final long classId, final int width, final int height, final ByteBuffer buffer, final int rotation);
// SourceRawDataInput
public static native long nativeSourceRawInputNew();
public static native void nativeSourceRawInputUploadBytes(final long classID, final int[] pixel, final int width, final int height, final int stride);
Expand All @@ -238,7 +243,7 @@ public static void copyAssetsToFiles(Context context, String oldPath, String ne
// Source
public static native long nativeSourceAddFilter(final long targetClassId);
public static native long nativeSourceAddTarget(final long classID, final long targetClassID, final int texID, final boolean isFilter);
public static native long nativeSourceAddTargetOutputCallback(final long classId);
public static native long nativeSourceAddTargetOutputCallback(final long classId, final RawOutputCallback callback);
public static native void nativeSourceRemoveTarget(final long classID, final long targetClassID, final boolean isFilter);
public static native void nativeSourceRemoveAllTargets(final long classID);
public static native boolean nativeSourceProceed(final long classID, final boolean bUpdateTargets);
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,11 +24,11 @@ public GPUPixelSource addTarget(GPUPixelTarget target) {
return addTarget(target, -1);
}

public void addTargetCallback() {
public void addTargetCallback(final GPUPixel.RawOutputCallback callback) {
GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
public void run() {
GPUPixel.nativeSourceAddTargetOutputCallback(mNativeClassID);
GPUPixel.nativeSourceAddTargetOutputCallback(mNativeClassID, callback);
}
});
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import android.view.SurfaceHolder;
import android.view.WindowManager;
import java.io.IOException;
import java.nio.ByteBuffer;
import java.nio.IntBuffer;

public class GPUPixelSourceCamera extends GPUPixelSource implements Camera.PreviewCallback {
Expand Down Expand Up @@ -73,6 +74,17 @@ public void run() {
proceed(true, true);
}

public void setFrameByBuffer(final ByteBuffer buffer, final int width, final int height) {
GPUPixel.getInstance().runOnDraw(new Runnable() {
@Override
public void run() {
GPUPixel.nativeSourceCameraSetFrameByBuffer(mNativeClassID, width, height, buffer, GPUPixel.NoRotation);
}
});

proceed(true, true);
}

@Override
public void onPreviewFrame(final byte[] data, Camera camera) {
final Camera.Size previewSize = camera.getParameters().getPreviewSize();
Expand Down
64 changes: 52 additions & 12 deletions src/android/jni/jni_gpupixel.cc
Original file line number Diff line number Diff line change
Expand Up @@ -91,6 +91,24 @@ extern "C" void Java_com_pixpark_gpupixel_GPUPixel_nativeSourceCameraSetFrame(
env->ReleaseIntArrayElements(jdata, data, 0);
};

extern "C" void Java_com_pixpark_gpupixel_GPUPixel_nativeSourceCameraSetFrameByBuffer(
JNIEnv* env,
jclass,
jlong classId,
jint width,
jint height,
jobject buffer,
jint rotation) {
// Get the pointer to the ByteBuffer data
void* pixels = env->GetDirectBufferAddress(buffer);
if (pixels == nullptr) {
// Handle error: Direct buffer address could not be obtained
return;
}
((SourceCamera*)classId)
->setFrameData(width, height, pixels, (RotationMode)rotation);
};

extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceRawInputNew(
JNIEnv* env,
jclass) {
Expand Down Expand Up @@ -189,22 +207,44 @@ extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTarget(
extern "C" jlong Java_com_pixpark_gpupixel_GPUPixel_nativeSourceAddTargetOutputCallback(
JNIEnv* env,
jclass,
jlong classId) {
jlong classId,
jobject src) {

jobject globalSourceRef = env->NewGlobalRef(src);

jclass cls = env->GetObjectClass(globalSourceRef);
jmethodID mid = env->GetMethodID(cls, "onRawOutput", "([BIII)V");

if (!mid) return 0;

Source* source = (Source*)classId;
// std::shared_ptr<Target> target =
// isFilter ? std::shared_ptr<Target>((Filter*)targetClassId)
// : std::shared_ptr<Target>((Target*)targetClassId);

std::shared_ptr<Target> target;

std::shared_ptr<TargetRawDataOutput> output = TargetRawDataOutput::create();
output->setPixelsCallbck([&](const uint8_t* data, int width, int height, int64_t ts) {
for (int i = 0; i < width * height; ++i) {
int a = data[i * 4 + 3]; // Alpha
int r = data[i * 4 + 0]; // Red
int g = data[i * 4 + 1]; // Green
int b = data[i * 4 + 2]; // Blue
__android_log_print(ANDROID_LOG_INFO, "OpenGL", "value: %d %d %d %d", a, r, g, b);
}

JavaVM* jvm;
env->GetJavaVM(&jvm);

output->setPixelsCallbck([=](const uint8_t* data, int width, int height, int64_t ts) {
size_t frame_size = width * height * 4;

JNIEnv* myNewEnv;
JavaVMAttachArgs args;
args.version = JNI_VERSION_1_6; // choose your JNI version
args.name = NULL; // you might want to give the java thread a name
args.group = NULL; // you might want to assign the java thread to a ThreadGroup
jvm->AttachCurrentThread(reinterpret_cast<JNIEnv **>((void **) &myNewEnv), &args);

jbyte* by = (jbyte*)data;

jbyteArray yArray = myNewEnv->NewByteArray(frame_size);

myNewEnv->SetByteArrayRegion(yArray, 0, frame_size, by);

myNewEnv->CallVoidMethod(globalSourceRef, mid, yArray, width, height, ts);

myNewEnv->DeleteLocalRef(yArray);
});
target = output;

Expand Down

0 comments on commit 0a6c06e

Please sign in to comment.