编译renderscript code运行时 [英] Compiling renderscript code at runtime

查看:297
本文介绍了编译renderscript code运行时的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想知道是否有可能写入/修改Android的应用程序运行时renderscript code。 我的主要目标是使一个应用程序,用户可以学会与renderscript工作,没有任何Java知识。该应用的基本布局将包括一个输入和输出图像的,并有可能code输入。这个应用程序的基本功能已经在工作,这是接口的图像。

I was wondering if it's possible to write/modify renderscript code when the android application is running. My main goal is to make an app where users can learn to work with renderscript without any java knowledge. The basic layout of the app would consist of an input and output image, with the possibility for code input. The basic functionality of this app is already working and this is an image of the interface.

这将是有益的测试renderscript code从应用程序的直接反馈。

It would be useful to test renderscript code with direct feedback from the app.

我已经做过研究关于构建过程,并提出了以下思路:

有一个基本的模板renderscript文件中,就像进出分配必要的全局变量。

A basic "template" renderscript file, with the necessary global variables like in and out allocations.

我的Java code会造成生成的Java类的脚本对象,做这些全局脚本变量的基本初始化。 此模板.RS文件的根功能将是空的,并且它应该是能够通过用户在运行时执行。

My java code would create a script object of the generated java class and do the basic initialization of these global script variables. The root function of this template .rs file would be empty, and it should be possible to be implemented by the user at runtime.

当他的code。在应用程序的主视图,用户类型,在code被写入到,然后编译的LLVM-RS-cc编译我自己.RS文件的调用应用。所产生的.BC然后将被复制到的原始.BC文件的位置 的模板。因为code,唯一的变化发生在根函数内部,它不会是必要更改周边 java的code。

When the user types in his code in the main view of the app, the code is written to my own .rs file which then is compiled by the llvm-rs-cc compiler, invoked by the application. The generated .bc would then be copied to the location of the original .bc file of the template. Because the only change of code happens inside the root function, it would not be necessary to make changes to the surrounding java code.

我在此刻的问题是一个事实,即.BC文件的最后的apk内捆绑内的资源/原始文件夹, 这不是由应用访问。覆盖旧.BC文件用新生成的文件,因此不可能的。

The problem I'm having at the moment is the fact that the .bc files are bundled within the final apk inside the res/raw folder, which is not accessible by the app. Overwriting old .bc files with the newly generated files is therefore not possible.

有没有一种(另)的方式在运行时编译renderscript code?

编辑:解决方案可以在此 github上的链接找到。欲了解更多详情,请这个的答案,请阅读最后一个注释

Solution can be found in this github link. For more details, check this answer, read the last comment

推荐答案

您将不得不改变所产生的胶类( ScriptC_mono ),它的底座( ScriptC )最起码。

You will have to alter the generated glue class (ScriptC_mono) and it's base (ScriptC) at the very least.

< ExtremeHax>

.BC 文件被读取并传递下来到renderScript的内部。到目前为止,我所知道的,那就是在<一做href="https://android.googlesource.com/platform/frameworks/support/+/refs/heads/master/v8/renderscript/java/src/android/support/v8/renderscript/ScriptC.java"相对=nofollow> ScriptC.internalCreate() ,这是有线读取原始资源。你需要它,而不是从自己控制的位置读取文件。也许你只需要修改 internalCreate(),但有可能会出现并发症,需要更广泛的编辑。

The .bc file is read and passed down to RenderScript's internals. So far as I can tell, that is done inside ScriptC.internalCreate(), which is wired to read a raw resource. You need it to instead read your file from a location you control. Possibly you will only need to modify internalCreate() but there will probably be complications requiring broader editing.

一旦实施 RuntimeScriptC ,您应该修改 ScriptC_mono 从该基类来代替<$ C $继承C> ScriptC 。

Once you implement RuntimeScriptC, you should modify ScriptC_mono to inherit from that base class instead of ScriptC.

正如你提到的,你会很有限,你可以对 RS code,因为你不能在运行时修改胶水类的变化。例如,你不能改变一个内核的签名。然而,你可以借此进一步破解了一点:如果你放弃了胶类,它可以让你想什么改变

As you noted, you will be very limited in the changes you can make to the rs code since you cannot modify the glue class at runtime. For example, you could not change the signature of a kernel. However you could take this hack a bit further: if you forgo the glue class, it is possible to make whatever changes you want.

内核在胶类通过调用索引,而不是按名称和类型/元素检查胶水类中完成的。因此,即使你只要改变内核的签名,因为你知道它的指数及其在进出分配的类型,你仍然可以使用的forEach()调用它直接

Kernels are invoked in the glue class by index rather than by name, and type/element checks are done inside the glue class. Thus even if you changed the kernel's signature, so long as you knew its index and the types of its in and out allocations, you could still invoke it by using forEach() directly.

public class ScriptC_mono extends ScriptC {
  //...

  public void forEach_root(Allocation ain, Allocation aout) {
    // check ain
    if (!ain.getType().getElement().isCompatible(__U8_4)) {
        throw new RSRuntimeException("Type mismatch with U8_4!");
    }
    // check aout
    if (!aout.getType().getElement().isCompatible(__U8_4)) {
        throw new RSRuntimeException("Type mismatch with U8_4!");
            // Verify dimensions
    Type tIn = ain.getType();
    Type tOut = aout.getType();
    if ((tIn.getCount() != tOut.getCount()) ||
        (tIn.getX() != tOut.getX()) ||
        (tIn.getY() != tOut.getY()) ||
        (tIn.getZ() != tOut.getZ()) ||
        (tIn.hasFaces() != tOut.hasFaces()) ||
        (tIn.hasMipmaps() != tOut.hasMipmaps())) {
        throw new RSRuntimeException("Dimension mismatch between input and output parameters!");
    }
    forEach(mExportForEachIdx_root, ain, aout, null);
  }
}

&LT; / ExtremeHax&GT;

这篇关于编译renderscript code运行时的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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