使用本机code完全(没有JNI)从后台线程更新UI? [英] Updating UI from background thread using native code entirely (no JNI)?

查看:242
本文介绍了使用本机code完全(没有JNI)从后台线程更新UI?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我写一个Android应用程序,做多大的处理在后台线程。计算是用C ++本地$ C $下进行。我想更新计算过程中的部分结果UI。

I am writing an Android application that does much of a processing in the background thread. The calculations are performed in the native code using C++. I want to update UI with the partial results during calculations.

我能够通过JNI这样做,即设置参考UI对象在Java中,然后调用这个对象从本地code槽JNI的方法。我要寻找一个更有效的解决方案,无需任何JNI在渲染部分调用(基本设置和激活切入点,将不得不在Java)。

I am able to do so through JNI, namely set reference to UI object in java and then call a method of this object from native code trough JNI. I am looking for a more efficient solution without any JNI calls in the rendering part (the basic setup and an activation entry point will have to be in java).

是否有可能执行线程与背景完全改变UI在本地code,无(只在渲染部分)使用JNI?

Is it possible to change execution thread from background to UI entirely in native code without using JNI (only in the rendering part)?

谢谢您的回答。大部分AP preciated。

Thanks for your answers. Much appreciated.

编辑:
我使用OpenGL渲染在我看来,计算出其含量(排序视频帧)考虑。在这种情况下,我可能会想使用 eglSwapBuffers()方法是EGL库可从2.3起。最大的问题是,我认为,如何轻松地从后台计算线程切换到UI开放GL线程本地code,而不JNI开销。你有什么建议?感谢您的帮助!

I am considering using OpenGL to render calculated content (sort of video frames) in my view. In that case I would probably want to use eglSwapBuffers() method that is available in EGL library from 2.3 onwards. The biggest problem is, I think, how to easily switch from background "calculation" thread to UI "open GL" thread in native code without JNI overhead. What would you recommend? Thanks for your help!

伪code:
下面是一些伪code,有助于说明我想在这里实现的目标。它更像是一个线程的问题,但Android框架进场为好。

PSEUDOCODE: Here is some pseudocode that helps to illustrate what I want to achieve here. It is more like a threading issue but Android framework comes into play as well.

// background thread
void Java_com_example_hellojni_HelloJni_stringFromJNI( JNIEnv* env,
                                                  jobject thiz)
{
    //long calculation
    struct result * res = calculate();

    //want to update ui using opengl on ui thread
    updateGL(res);
}


//want to run it on ui thread as opengl can be run only on ui thread
void updateGL(struct result * res)
{
    //some opengl code with "res"
    someGLcodeWithResult(res);

    //render surface
    eglSwapBuffers();
}

编辑2:
作为赏金慢慢即将关闭,多了一个澄清。有两种方法可以拨打以上 updateGL 方法。最典型的是在java中code使用 GLSurfaceView 。它需要设置渲染器(<一个href=\"http://developer.android.com/reference/android/opengl/GLSurfaceView.html#setRenderer%28android.opengl.GLSurfaceView.Renderer%29\"标题=setRenderer()> setRenderer()),然后覆盖<一个href=\"http://developer.android.com/reference/android/opengl/GLSurfaceView.Renderer.html#onDrawFrame%28javax.microedition.khronos.opengles.GL10%29\">onDrawFrame一些code调用JNI /原生层。通过这种方式,每帧渲染1 JNI调用被执行。

EDIT 2: As the bounty slowly draws to close, one more clarification. There are couple of ways to call the updateGL method above. The most typical one is to use GLSurfaceView in java code. It would require setting a renderer (setRenderer()) and then overriding onDrawFrame with some code to call JNI/native layer. This way for every frame being rendered one JNI call gets executed.

我愿做渲染有点不同。我想打电话给 updateGL 不使用的Java / JNI code。在所有与只使用原生层(如上面的摘录psented $ P $)。对我来说,现在最大的问题就是如何在不java的回调在UI线程(由OpenGL的要求)运行 updateGL 。它是在所有可行吗?

I would like to do the rendering a little differently. I want to call updateGL without using java/JNI code at all and use only the native layer (as presented in the excerpt above). The biggest problem for me now is how to run the updateGL on UI thread (required by OpenGL) without the java callback. Is it at all feasible?

推荐答案

我面临同样的问题现在。或许,这可能会帮助:

I'm facing the same problem right now. Perhaps this might help:

当你看看在最新的NDK稳定,APIS.html它说:

When you take a look at the STABLE-APIS.html in the latest NDK it says:

下面列出的所有API可用于开发运行原生code
在Android> 2.3系统映像及以上。

All the APIs listed below are available for developing native code that runs on Android > 2.3 system images and above.

EGL提供了一个原生平台接口来分配和管理OpenGLES
表面。有关其功能的更多信息,请访问:

EGL provides a native platform interface to allocate and manage OpenGLES surfaces. For more information about its features, please see:

http://www.khronos.org/egl

在简单地说,这将允许你这样做直接从以下
本土code:

In a nutshell, this will allow you to do the following directly from native code:

- List supported EGL configurations
- Allocate and release OpenGLES surfaces
- Swap/Flip surfaces for display  (eglSwapBuffers)

所以,如果你的目标Android系统的9,那么你可以尝试使用EGL的lib在本地code。
(让我知道,如果这个工程:))

So if you're targeting Android-9 then you could try to use the EGL lib in native code. (Let me know if this works :))

这篇关于使用本机code完全(没有JNI)从后台线程更新UI?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆