Donate to e Foundation | Murena handsets with /e/OS | Own a part of Murena! Learn more

Commit 4d339933 authored by Jason Sams's avatar Jason Sams
Browse files

Convert renderscript from using ACC to LLVM for its compiler.

This will also require application to be updated to support
the new compiler and data passing models.

Change-Id: If078e3a5148af395ba1b936169a407d8c3ad727f
parent 810f5ccb
Loading
Loading
Loading
Loading
+12 −5
Original line number Diff line number Diff line
@@ -138,16 +138,17 @@ public class RenderScript {
    native void nScriptSetClearDepth(int script, float depth);
    native void nScriptSetClearStencil(int script, int stencil);
    native void nScriptSetTimeZone(int script, byte[] timeZone);
    native void nScriptSetType(int type, boolean writable, String name, int slot);
    native void nScriptSetRoot(boolean isRoot);
    native void nScriptSetInvokable(String name, int slot);
    native void nScriptInvoke(int id, int slot);
    native void nScriptInvokeData(int id, int slot);
    native void nScriptInvokeV(int id, int slot, byte[] params);
    native void nScriptSetVarI(int id, int slot, int val);
    native void nScriptSetVarF(int id, int slot, float val);
    native void nScriptSetVarV(int id, int slot, byte[] val);

    native void nScriptCBegin();
    native void nScriptCSetScript(byte[] script, int offset, int length);
    native int  nScriptCCreate();
    native void nScriptCAddDefineI32(String name, int value);
    native void nScriptCAddDefineF(String name, float value);

    native void nSamplerBegin();
    native void nSamplerSet(int param, int value);
@@ -229,6 +230,13 @@ public class RenderScript {
    Element mElement_COLOR_U8_4;
    Element mElement_COLOR_F32_4;

    Sampler mSampler_CLAMP_NEAREST;
    Sampler mSampler_CLAMP_LINEAR;
    Sampler mSampler_CLAMP_LINEAR_MIP_LINEAR;
    Sampler mSampler_WRAP_NEAREST;
    Sampler mSampler_WRAP_LINEAR;
    Sampler mSampler_WRAP_LINEAR_MIP_LINEAR;

    ///////////////////////////////////////////////////////////////////////////////////
    //

@@ -293,7 +301,6 @@ public class RenderScript {
                    mRS.mMessageCallback.mID = msg;
                    mRS.mMessageCallback.run();
                }
                //Log.d(LOG_TAG, "MessageThread msg " + msg + " v1 " + rbuf[0] + " v2 " + rbuf[1] + " v3 " +rbuf[2]);
            }
            Log.d(LOG_TAG, "MessageThread exiting.");
        }
+80 −0
Original line number Diff line number Diff line
@@ -51,6 +51,86 @@ public class Sampler extends BaseObj {
        mID = id;
    }

    Sampler mSampler_CLAMP_NEAREST;
    Sampler mSampler_CLAMP_LINEAR;
    Sampler mSampler_CLAMP_LINEAR_MIP;
    Sampler mSampler_WRAP_NEAREST;
    Sampler mSampler_WRAP_LINEAR;
    Sampler mSampler_WRAP_LINEAR_MIP;

    public static Sampler CLAMP_NEAREST(RenderScript rs) {
        if(rs.mSampler_CLAMP_NEAREST == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.NEAREST);
            b.setMag(Value.NEAREST);
            b.setWrapS(Value.CLAMP);
            b.setWrapT(Value.CLAMP);
            rs.mSampler_CLAMP_NEAREST = b.create();
        }
        return rs.mSampler_CLAMP_NEAREST;
    }

    public static Sampler CLAMP_LINEAR(RenderScript rs) {
        if(rs.mSampler_CLAMP_LINEAR == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.LINEAR);
            b.setMag(Value.LINEAR);
            b.setWrapS(Value.CLAMP);
            b.setWrapT(Value.CLAMP);
            rs.mSampler_CLAMP_LINEAR = b.create();
        }
        return rs.mSampler_CLAMP_LINEAR;
    }

    public static Sampler CLAMP_LINEAR_MIP_LINEAR(RenderScript rs) {
        if(rs.mSampler_CLAMP_LINEAR_MIP_LINEAR == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.LINEAR_MIP_LINEAR);
            b.setMag(Value.LINEAR_MIP_LINEAR);
            b.setWrapS(Value.CLAMP);
            b.setWrapT(Value.CLAMP);
            rs.mSampler_CLAMP_LINEAR_MIP_LINEAR = b.create();
        }
        return rs.mSampler_CLAMP_LINEAR_MIP_LINEAR;
    }

    public static Sampler WRAP_NEAREST(RenderScript rs) {
        if(rs.mSampler_WRAP_NEAREST == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.NEAREST);
            b.setMag(Value.NEAREST);
            b.setWrapS(Value.WRAP);
            b.setWrapT(Value.WRAP);
            rs.mSampler_WRAP_NEAREST = b.create();
        }
        return rs.mSampler_WRAP_NEAREST;
    }

    public static Sampler WRAP_LINEAR(RenderScript rs) {
        if(rs.mSampler_WRAP_LINEAR == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.LINEAR);
            b.setMag(Value.LINEAR);
            b.setWrapS(Value.WRAP);
            b.setWrapT(Value.WRAP);
            rs.mSampler_WRAP_LINEAR = b.create();
        }
        return rs.mSampler_WRAP_LINEAR;
    }

    public static Sampler WRAP_LINEAR_MIP_LINEAR(RenderScript rs) {
        if(rs.mSampler_WRAP_LINEAR_MIP_LINEAR == null) {
            Builder b = new Builder(rs);
            b.setMin(Value.LINEAR_MIP_LINEAR);
            b.setMag(Value.LINEAR_MIP_LINEAR);
            b.setWrapS(Value.WRAP);
            b.setWrapT(Value.WRAP);
            rs.mSampler_WRAP_LINEAR_MIP_LINEAR = b.create();
        }
        return rs.mSampler_WRAP_LINEAR_MIP_LINEAR;
    }


    public static class Builder {
        RenderScript mRS;
        Value mMin;
+26 −62
Original line number Diff line number Diff line
@@ -46,6 +46,15 @@ public class Script extends BaseObj {
        mRS.nScriptInvoke(mID, slot);
    }

    protected void invokeData(int slot) {
        mRS.nScriptInvokeData(mID, slot);
    }

    protected void invokeV(int slot, FieldPacker v) {
        mRS.nScriptInvokeV(mID, slot, v.getData());
    }


    Script(int id, RenderScript rs) {
        super(rs);
        mID = id;
@@ -53,7 +62,23 @@ public class Script extends BaseObj {

    public void bindAllocation(Allocation va, int slot) {
        mRS.validate();
        if (va != null) {
            mRS.nScriptBindAllocation(mID, va.mID, slot);
        } else {
            mRS.nScriptBindAllocation(mID, 0, slot);
        }
    }

    public void setVar(int index, float v) {
        mRS.nScriptSetVarF(mID, index, v);
    }

    public void setVar(int index, int v) {
        mRS.nScriptSetVarI(mID, index, v);
    }

    public void setVar(int index, FieldPacker v) {
        mRS.nScriptSetVarV(mID, index, v.getData());
    }

    public void setClearColor(float r, float g, float b, float a) {
@@ -82,71 +107,10 @@ public class Script extends BaseObj {

    public static class Builder {
        RenderScript mRS;
        boolean mIsRoot = false;
        Type[] mTypes;
        String[] mNames;
        boolean[] mWritable;
        int mInvokableCount = 0;
        Invokable[] mInvokables;

        Builder(RenderScript rs) {
            mRS = rs;
            mTypes = new Type[MAX_SLOT];
            mNames = new String[MAX_SLOT];
            mWritable = new boolean[MAX_SLOT];
            mInvokables = new Invokable[MAX_SLOT];
        }

        public void setType(Type t, int slot) {
            mTypes[slot] = t;
            mNames[slot] = null;
        }

        public void setType(Type t, String name, int slot) {
            mTypes[slot] = t;
            mNames[slot] = name;
        }

        public Invokable addInvokable(String func) {
            Invokable i = new Invokable();
            i.mName = func;
            i.mRS = mRS;
            i.mSlot = mInvokableCount;
            mInvokables[mInvokableCount++] = i;
            return i;
        }

        public void setType(boolean writable, int slot) {
            mWritable[slot] = writable;
        }

        void transferCreate() {
            mRS.nScriptSetRoot(mIsRoot);
            for(int ct=0; ct < mTypes.length; ct++) {
                if(mTypes[ct] != null) {
                    mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
                }
            }
            for(int ct=0; ct < mInvokableCount; ct++) {
                mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
            }
        }

        void transferObject(Script s) {
            s.mIsRoot = mIsRoot;
            s.mTypes = mTypes;
            s.mInvokables = new Invokable[mInvokableCount];
            for(int ct=0; ct < mInvokableCount; ct++) {
                s.mInvokables[ct] = mInvokables[ct];
                s.mInvokables[ct].mScript = s;
            }
            s.mInvokables = null;
        }

        public void setRoot(boolean r) {
            mIsRoot = r;
        }

    }


+6 −54
Original line number Diff line number Diff line
@@ -81,8 +81,6 @@ public class ScriptC extends Script {
    public static class Builder extends Script.Builder {
        byte[] mProgram;
        int mProgramLength;
        HashMap<String,Integer> mIntDefines = new HashMap();
        HashMap<String,Float> mFloatDefines = new HashMap();

        public Builder(RenderScript rs) {
            super(rs);
@@ -133,66 +131,20 @@ public class ScriptC extends Script {

        static synchronized ScriptC internalCreate(Builder b) {
            b.mRS.nScriptCBegin();
            b.transferCreate();

            for (Entry<String,Integer> e: b.mIntDefines.entrySet()) {
                b.mRS.nScriptCAddDefineI32(e.getKey(), e.getValue().intValue());
            }
            for (Entry<String,Float> e: b.mFloatDefines.entrySet()) {
                b.mRS.nScriptCAddDefineF(e.getKey(), e.getValue().floatValue());
            }

            android.util.Log.e("rs", "len = " + b.mProgramLength);
            b.mRS.nScriptCSetScript(b.mProgram, 0, b.mProgramLength);

            int id = b.mRS.nScriptCCreate();
            ScriptC obj = new ScriptC(id, b.mRS);
            b.transferObject(obj);

            return obj;
        }

        public void addDefine(String name, int value) {
            mIntDefines.put(name, value);
        }

        public void addDefine(String name, float value) {
            mFloatDefines.put(name, value);
        }

        /**
         * Takes the all public static final fields for a class, and adds defines
         * for them, using the name of the field as the name of the define.
         */
        public void addDefines(Class cl) {
            addDefines(cl.getFields(), (Modifier.STATIC | Modifier.FINAL | Modifier.PUBLIC), null);
        }

        /**
         * Takes the all public fields for an object, and adds defines
         * for them, using the name of the field as the name of the define.
         */
        public void addDefines(Object o) {
            addDefines(o.getClass().getFields(), Modifier.PUBLIC, o);
        }

        void addDefines(Field[] fields, int mask, Object o) {
            for (Field f: fields) {
                try {
                    if ((f.getModifiers() & mask) == mask) {
                        Class t = f.getType();
                        if (t == int.class) {
                            mIntDefines.put(f.getName(), f.getInt(o));
                        }
                        else if (t == float.class) {
                            mFloatDefines.put(f.getName(), f.getFloat(o));
                        }
                    }
                } catch (IllegalAccessException ex) {
                    // TODO: Do we want this log?
                    Log.d(TAG, "addDefines skipping field " + f.getName());
                }
            }
        }
        public void addDefine(String name, int value) {}
        public void addDefine(String name, float value) {}
        public void addDefines(Class cl) {}
        public void addDefines(Object o) {}
        void addDefines(Field[] fields, int mask, Object o) {}

        public ScriptC create() {
            return internalCreate(this);
+45 −27
Original line number Diff line number Diff line
@@ -853,6 +853,33 @@ nScriptBindAllocation(JNIEnv *_env, jobject _this, jint script, jint alloc, jint
    rsScriptBindAllocation(con, (RsScript)script, (RsAllocation)alloc, slot);
}

static void
nScriptSetVarI(JNIEnv *_env, jobject _this, jint script, jint slot, jint val)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
    rsScriptSetVarI(con, (RsScript)script, slot, val);
}

static void
nScriptSetVarF(JNIEnv *_env, jobject _this, jint script, jint slot, float val)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptSetVarI, con(%p), s(%p), slot(%i), val(%i), b(%f), a(%f)", con, (void *)script, slot, val);
    rsScriptSetVarF(con, (RsScript)script, slot, val);
}

static void
nScriptSetVarV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptSetVarV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
    jint len = _env->GetArrayLength(data);
    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
    rsScriptSetVarV(con, (RsScript)script, slot, ptr, len);
    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
}

static void
nScriptSetClearColor(JNIEnv *_env, jobject _this, jint script, jfloat r, jfloat g, jfloat b, jfloat a)
{
@@ -895,41 +922,31 @@ nScriptSetTimeZone(JNIEnv *_env, jobject _this, jint script, jbyteArray timeZone
}

static void
nScriptSetType(JNIEnv *_env, jobject _this, jint type, jboolean writable, jstring _str, jint slot)
nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptCAddType, con(%p), type(%p), writable(%i), slot(%i)", con, (RsType)type, writable, slot);
    const char* n = NULL;
    if (_str) {
        n = _env->GetStringUTFChars(_str, NULL);
    }
    rsScriptSetType(con, (RsType)type, slot, writable, n);
    if (n) {
        _env->ReleaseStringUTFChars(_str, n);
    }
    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
    rsScriptInvoke(con, (RsScript)obj, slot);
}

static void
nScriptSetInvoke(JNIEnv *_env, jobject _this, jstring _str, jint slot)
nScriptInvokeData(JNIEnv *_env, jobject _this, jint obj, jint slot)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptSetInvoke, con(%p)", con);
    const char* n = NULL;
    if (_str) {
        n = _env->GetStringUTFChars(_str, NULL);
    }
    rsScriptSetInvoke(con, n, slot);
    if (n) {
        _env->ReleaseStringUTFChars(_str, n);
    }
    LOG_API("nScriptInvokeData, con(%p), script(%p)", con, (void *)obj);
    rsScriptInvokeData(con, (RsScript)obj, slot, 0);
}


static void
nScriptInvoke(JNIEnv *_env, jobject _this, jint obj, jint slot)
nScriptInvokeV(JNIEnv *_env, jobject _this, jint script, jint slot, jbyteArray data)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nScriptInvoke, con(%p), script(%p)", con, (void *)obj);
    rsScriptInvoke(con, (RsScript)obj, slot);
    LOG_API("nScriptInvokeV, con(%p), s(%p), slot(%i)", con, (void *)script, slot);
    jint len = _env->GetArrayLength(data);
    jbyte *ptr = _env->GetByteArrayElements(data, NULL);
    rsScriptInvokeV(con, (RsScript)script, slot, ptr, len);
    _env->ReleaseByteArrayElements(data, ptr, JNI_ABORT);
}

static void
@@ -1424,16 +1441,17 @@ static JNINativeMethod methods[] = {
{"nScriptSetClearDepth",           "(IF)V",                                (void*)nScriptSetClearDepth },
{"nScriptSetClearStencil",         "(II)V",                                (void*)nScriptSetClearStencil },
{"nScriptSetTimeZone",             "(I[B)V",                               (void*)nScriptSetTimeZone },
{"nScriptSetType",                 "(IZLjava/lang/String;I)V",             (void*)nScriptSetType },
{"nScriptSetRoot",                 "(Z)V",                                 (void*)nScriptSetRoot },
{"nScriptSetInvokable",            "(Ljava/lang/String;I)V",               (void*)nScriptSetInvoke },
{"nScriptInvoke",                  "(II)V",                                (void*)nScriptInvoke },
{"nScriptInvokeData",              "(II)V",                                (void*)nScriptInvokeData },
{"nScriptInvokeV",                 "(II[B)V",                              (void*)nScriptInvokeV },
{"nScriptSetVarI",                 "(III)V",                               (void*)nScriptSetVarI },
{"nScriptSetVarF",                 "(IIF)V",                               (void*)nScriptSetVarF },
{"nScriptSetVarV",                 "(II[B)V",                              (void*)nScriptSetVarV },

{"nScriptCBegin",                  "()V",                                  (void*)nScriptCBegin },
{"nScriptCSetScript",              "([BII)V",                              (void*)nScriptCSetScript },
{"nScriptCCreate",                 "()I",                                  (void*)nScriptCCreate },
{"nScriptCAddDefineI32",           "(Ljava/lang/String;I)V",               (void*)nScriptCAddDefineI32 },
{"nScriptCAddDefineF",             "(Ljava/lang/String;F)V",               (void*)nScriptCAddDefineF },

{"nProgramFragmentStoreBegin",     "(II)V",                                (void*)nProgramFragmentStoreBegin },
{"nProgramFragmentStoreDepthFunc", "(I)V",                                 (void*)nProgramFragmentStoreDepthFunc },
Loading