Skip to content

Commit

Permalink
Merge pull request #5 from pixpark/dev
Browse files Browse the repository at this point in the history
Add native layer to obtain app context on Android, optimize the creation of Android source images.
  • Loading branch information
jaaronkot authored Dec 22, 2023
2 parents a420731 + a99530f commit e80196f
Show file tree
Hide file tree
Showing 5 changed files with 84 additions and 61 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -45,8 +45,6 @@ protected void onCreate(Bundle savedInstanceState) {
// 保持屏幕常亮
getWindow().addFlags(WindowManager.LayoutParams.FLAG_KEEP_SCREEN_ON);

// must call fist
GPUPixel.setAppContext(this);
// preview
surfaceView = findViewById(R.id.surfaceView);

Expand Down
10 changes: 0 additions & 10 deletions android/gpupixel/src/main/java/com/pixpark/gpupixel/GPUPixel.java
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,6 @@

package com.pixpark.gpupixel;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.graphics.PixelFormat;
Expand All @@ -23,7 +22,6 @@ public class GPUPixel {
public static final int RotateRightFlipHorizontal = 6;
public static final int Rotate180 = 7;

private static Context context_;
private GPUPixelRenderer mRenderer = null;
private GLSurfaceView mGLSurfaceView = null;
private int mGLSurfaceViewRenderMode = GLSurfaceView.RENDERMODE_WHEN_DIRTY;
Expand All @@ -42,14 +40,6 @@ public static final GPUPixel getInstance() {
return instance;
}

public static void setAppContext(Context context) {
context_ = context;
}

public static Context getAppContext() {
return context_;
}

public boolean isInited() {
return mRenderer != null;
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -88,20 +88,20 @@ public void run() {
}
}

public static Bitmap createBitmap(String img_name) {
public static Bitmap createBitmap(Context context, String img_name) {
Bitmap bitmap = null;
try {
bitmap = BitmapFactory.decodeStream(GPUPixel.getAppContext().getAssets().open(img_name));
bitmap = BitmapFactory.decodeStream(context.getAssets().open(img_name));
} catch (IOException e) {
e.printStackTrace();
}

return bitmap;
}

private int kkkkk(String img_name) {
private int createTexture(Context context, String img_name) {
try {
bitmap = BitmapFactory.decodeStream(GPUPixel.getAppContext().getAssets().open(img_name));
bitmap = BitmapFactory.decodeStream(context.getAssets().open(img_name));
int[] textureIds = new int[1];
GLES20.glGenTextures(1, textureIds, 0);
int error;
Expand Down
120 changes: 75 additions & 45 deletions src/source/SourceImage.cc
Original file line number Diff line number Diff line change
Expand Up @@ -8,32 +8,76 @@
#include "SourceImage.h"
#include "GPUPixelContext.h"
#include "Util.h"

#if defined(GPUPIXEL_ANDROID)

#include <android/bitmap.h>

#include "jni_helpers.h"

#endif

USING_NS_GPUPIXEL

std::shared_ptr<SourceImage> SourceImage::create(int width,
int height,
const void* pixels) {
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
sourceImage->setImage(width, height, pixels);
return sourceImage;
const void *pixels) {
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
sourceImage->setImage(width, height, pixels);
return sourceImage;
}

std::shared_ptr<SourceImage> SourceImage::create(const std::string name) {
#if defined(GPUPIXEL_ANDROID)
auto sourceImage = createImageForAndroid(name);
return sourceImage;
#elif defined(GPUPIXEL_MAC)
// Todo(Jeayo)
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
return sourceImage;
#elif defined(GPUPIXEL_IOS)
auto path = Util::getResourcePath(name);
UIImage* img =
[UIImage imageNamed:[NSString stringWithUTF8String:path.c_str()]];
return SourceImage::create(img);

#elif defined(GPUPIXEL_WIN)

#else
#endif
}

SourceImage::~SourceImage() {

}

SourceImage *SourceImage::setImage(int width, int height, const void *pixels) {
this->setFramebuffer(0);
if (!_framebuffer || (_framebuffer->getWidth() != width ||
_framebuffer->getHeight() != height)) {
_framebuffer =
GPUPixelContext::getInstance()->getFramebufferCache()->fetchFramebuffer(
width, height, true);
}
this->setFramebuffer(_framebuffer);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, this->getFramebuffer()->getTexture()));
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, pixels));
CHECK_GL(glBindTexture(GL_TEXTURE_2D, 0));
return this;
}

#if defined(GPUPIXEL_ANDROID)
std::shared_ptr<SourceImage> SourceImage::createImageForAndroid(std::string name) {
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
// Todo(Jeayo) @see https://developer.android.com/ndk/guides/image-decoder?hl=zh-cn
JavaVM *jvm = GetJVM();
JNIEnv* env = GetEnv(jvm);
JNIEnv *env = GetEnv(jvm);

// 定义类路径和方法签名
const char *className = "com/pixpark/gpupixel/GPUPixelSourceImage";
const char *methodName = "createBitmap";
const char *methodSignature = "(Ljava/lang/String;)Landroid/graphics/Bitmap;";
const char *methodSignature = "(Landroid/content/Context;Ljava/lang/String;)Landroid/graphics/Bitmap;";

// GPUPixelSourceImage
jclass jSourceImageClass = env->FindClass(className);
Expand All @@ -42,21 +86,40 @@ std::shared_ptr<SourceImage> SourceImage::create(const std::string name) {
}

// 找到createBitmap方法
jmethodID createBitmapMethod = env->GetStaticMethodID(jSourceImageClass, methodName, methodSignature);
jmethodID createBitmapMethod = env->GetStaticMethodID(jSourceImageClass, methodName,
methodSignature);
if (createBitmapMethod == NULL) {
return NULL;
}

// get app context
jobject jAppContext = NULL;
const char *context_name = "android/app/ActivityThread";
jclass activity_thread_clz = env->FindClass(context_name);
if (activity_thread_clz != NULL) {
jmethodID currentApplication = env->GetStaticMethodID(
activity_thread_clz, "currentApplication", "()Landroid/app/Application;");
if (currentApplication != NULL) {
jAppContext = env->CallStaticObjectMethod(activity_thread_clz, currentApplication);
} else {
//todo
}
env->DeleteLocalRef(activity_thread_clz);
} else {
// todo
}
// 创建Java字符串作为参数
jstring imgName = env->NewStringUTF(name.c_str());

// 调用createBitmap方法
jobject bitmap = env->CallStaticObjectMethod(jSourceImageClass, createBitmapMethod, imgName);
jobject bitmap = env->CallStaticObjectMethod(jSourceImageClass,
createBitmapMethod,
jAppContext,
imgName);

AndroidBitmapInfo info;
GLvoid* pixels;
GLvoid *pixels;
AndroidBitmap_getInfo(env, bitmap, &info);
if(info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
if (info.format != ANDROID_BITMAP_FORMAT_RGBA_8888) {
return nullptr;
}
AndroidBitmap_lockPixels(env, bitmap, &pixels);
Expand All @@ -67,42 +130,9 @@ std::shared_ptr<SourceImage> SourceImage::create(const std::string name) {
env->DeleteLocalRef(imgName);
env->DeleteLocalRef(jSourceImageClass);
return sourceImage;
#elif defined(GPUPIXEL_MAC)
// Todo(Jeayo)
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
return sourceImage;
#elif defined(GPUPIXEL_IOS)
auto path = Util::getResourcePath(name);
UIImage* img =
[UIImage imageNamed:[NSString stringWithUTF8String:path.c_str()]];
return SourceImage::create(img);

#elif defined(GPUPIXEL_WIN)

#else
#endif
}

SourceImage::~SourceImage() {

}

SourceImage* SourceImage::setImage(int width, int height, const void* pixels) {
this->setFramebuffer(0);
if (!_framebuffer || (_framebuffer->getWidth() != width ||
_framebuffer->getHeight() != height)) {
_framebuffer =
GPUPixelContext::getInstance()->getFramebufferCache()->fetchFramebuffer(
width, height, true);
}
this->setFramebuffer(_framebuffer);
CHECK_GL(glBindTexture(GL_TEXTURE_2D, this->getFramebuffer()->getTexture()));
CHECK_GL(glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA, width, height, 0, GL_RGBA,
GL_UNSIGNED_BYTE, pixels));
CHECK_GL(glBindTexture(GL_TEXTURE_2D, 0));
return this;
}

#endif
#if defined(GPUPIXEL_IOS)
std::shared_ptr<SourceImage> SourceImage::create(NSURL* imageUrl) {
auto sourceImage = std::shared_ptr<SourceImage>(new SourceImage());
Expand Down
5 changes: 5 additions & 0 deletions src/source/SourceImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -33,10 +33,15 @@ class SourceImage : public Source {

static std::shared_ptr<SourceImage> create(CGImageRef image);
SourceImage* setImage(CGImageRef image);
#endif

private:
#if defined(GPUPIXEL_IOS)
UIImage* _adjustImageOrientation(UIImage* image);
#endif
#if defined(GPUPIXEL_ANDROID)
static std::shared_ptr<SourceImage> createImageForAndroid(std::string name);
#endif
};

NS_GPUPIXEL_END

0 comments on commit e80196f

Please sign in to comment.