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

Commit 1fe9b8c3 authored by Jason Sams's avatar Jason Sams
Browse files

Split FountainView into View and RS parts. Beging adding ProgramVertex to the...

Split FountainView into View and RS parts.  Beging adding ProgramVertex to the java api.  It was already implemented in native.
parent d5680f9b
Loading
Loading
Loading
Loading
+0 −1
Original line number Diff line number Diff line
@@ -35,7 +35,6 @@ int main(void* con, int ft, int launchID) {
        }
    }

    //contextBindProgramFragment(con, loadI32(con, 0, 7));
    drawRect(con, 0, 256, 0, 512);
    contextBindProgramFragment(con, NAMED_PgmFragParts);

+140 −0
Original line number Diff line number Diff line
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.fountain;

import java.io.Writer;

import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScript;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.util.AttributeSet;
import android.util.Log;

public class FountainRS {

    public FountainRS() {
    }

    public void init(RenderScript rs, Resources res, int width, int height) {
        mRS = rs;
        mRes = res;
        initRS();
    }

    public void newTouchPosition(int x, int y) {
        mParams[0] = 1;
        mParams[1] = x;
        mParams[2] = y;
        mIntAlloc.subData1D(2, 3, mParams);
    }


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

    private Resources mRes;

    private RenderScript mRS;
    private RenderScript.Allocation mIntAlloc;
    private RenderScript.Allocation mPartAlloc;
    private RenderScript.Allocation mVertAlloc;
    private RenderScript.Script mScript;
    private RenderScript.ProgramFragmentStore mPFS;
    private RenderScript.ProgramFragment mPF;
    private RenderScript.ProgramFragment mPF2;
    private RenderScript.Allocation mTexture;
    private RenderScript.Sampler mSampler;

    private Bitmap mBackground;

    int mParams[] = new int[10];

    private void initRS() {
        int partCount = 1024;

        mIntAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 10);
        mPartAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 3 * 3);
        mPartAlloc.setName("PartBuffer");
        mVertAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 5 + 1);

        {
            Drawable d = mRes.getDrawable(R.drawable.gadgets_clock_mp3);
            BitmapDrawable bd = (BitmapDrawable)d;
            Bitmap b = bd.getBitmap();
            mTexture = mRS.allocationCreateFromBitmap(b,
                                                      RenderScript.ElementPredefined.RGB_565,
                                                      true);
            mTexture.uploadToTexture(0);
        }

        mRS.programFragmentStoreBegin(null, null);
        mRS.programFragmentStoreBlendFunc(RenderScript.BlendSrcFunc.SRC_ALPHA, RenderScript.BlendDstFunc.ONE);
        mRS.programFragmentStoreDepthFunc(RenderScript.DepthFunc.ALWAYS);
        mPFS = mRS.programFragmentStoreCreate();
        mPFS.setName("MyBlend");
        mRS.contextBindProgramFragmentStore(mPFS);

        mRS.samplerBegin();
        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MAG, RenderScript.SamplerValue.LINEAR);
        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MIN, RenderScript.SamplerValue.LINEAR);
        mSampler = mRS.samplerCreate();


        mRS.programFragmentBegin(null, null);
        mPF = mRS.programFragmentCreate();
        mPF.setName("PgmFragParts");

        mRS.programFragmentBegin(null, null);
        mRS.programFragmentSetTexEnable(0, true);
        mPF2 = mRS.programFragmentCreate();
        mRS.contextBindProgramFragment(mPF2);
        mPF2.bindTexture(mTexture, 0);
        mPF2.bindSampler(mSampler, 0);
        mPF2.setName("PgmFragBackground");

        mParams[0] = 0;
        mParams[1] = partCount;
        mParams[2] = 0;
        mParams[3] = 0;
        mParams[4] = 0;
        mIntAlloc.data(mParams);

        int t2[] = new int[partCount * 4*3];
        for (int ct=0; ct < t2.length; ct++) {
            t2[ct] = 0;
        }
        mPartAlloc.data(t2);

        mRS.scriptCBegin();
        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        mRS.scriptCSetScript(mRes, R.raw.fountain);
        mRS.scriptCSetRoot(true);
        mScript = mRS.scriptCCreate();

        mScript.bindAllocation(mIntAlloc, 0);
        mScript.bindAllocation(mPartAlloc, 1);
        mScript.bindAllocation(mVertAlloc, 2);
        mRS.contextBindRootScript(mScript);
    }

}

+6 −106
Original line number Diff line number Diff line
@@ -47,126 +47,26 @@ public class FountainView extends RSSurfaceView {
    }

    private RenderScript mRS;
    private RenderScript.Allocation mIntAlloc;
    private RenderScript.Allocation mPartAlloc;
    private RenderScript.Allocation mVertAlloc;
    private RenderScript.Script mScript;
    private RenderScript.ProgramFragmentStore mPFS;
    private RenderScript.ProgramFragment mPF;
    private RenderScript.ProgramFragment mPF2;
    private RenderScript.Allocation mTexture;
    private RenderScript.Sampler mSampler;

    private Bitmap mBackground;

    int mParams[] = new int[10];

    private void initRS() {
        mRS = createRenderScript();

        int partCount = 1024;

        mIntAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, 10);
        mPartAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 3 * 3);
        mPartAlloc.setName("PartBuffer");
        mVertAlloc = mRS.allocationCreatePredefSized(RenderScript.ElementPredefined.USER_I32, partCount * 5 + 1);

        {
            Resources res = getResources();
            Drawable d = res.getDrawable(R.drawable.gadgets_clock_mp3);
            BitmapDrawable bd = (BitmapDrawable)d;
            Bitmap b = bd.getBitmap();
            mTexture = mRS.allocationCreateFromBitmap(b,
                                                      RenderScript.ElementPredefined.RGB_565,
                                                      true);
            mTexture.uploadToTexture(0);
        }

        mRS.programFragmentStoreBegin(null, null);
        mRS.programFragmentStoreBlendFunc(RenderScript.BlendSrcFunc.SRC_ALPHA, RenderScript.BlendDstFunc.ONE);
        mRS.programFragmentStoreDepthFunc(RenderScript.DepthFunc.ALWAYS);
        mPFS = mRS.programFragmentStoreCreate();
        mPFS.setName("MyBlend");
        mRS.contextBindProgramFragmentStore(mPFS);

        mRS.samplerBegin();
        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MAG, RenderScript.SamplerValue.LINEAR);
        mRS.samplerSet(RenderScript.SamplerParam.FILTER_MIN, RenderScript.SamplerValue.LINEAR);
        mSampler = mRS.samplerCreate();


        mRS.programFragmentBegin(null, null);
        mPF = mRS.programFragmentCreate();
        mPF.setName("PgmFragParts");

        mRS.programFragmentBegin(null, null);
        mRS.programFragmentSetTexEnable(0, true);
        mPF2 = mRS.programFragmentCreate();
        mRS.contextBindProgramFragment(mPF2);
        mPF2.bindTexture(mTexture, 0);
        mPF2.bindSampler(mSampler, 0);
        mPF2.setName("PgmFragBackground");

        mParams[0] = 0;
        mParams[1] = partCount;
        mParams[2] = 0;
        mParams[3] = 0;
        mParams[4] = 0;
        mParams[5] = mPartAlloc.getID();
        mIntAlloc.data(mParams);

        int t2[] = new int[partCount * 4*3];
        for (int ct=0; ct < t2.length; ct++) {
            t2[ct] = 0;
        }
        mPartAlloc.data(t2);

        mRS.scriptCBegin();
        mRS.scriptCSetClearColor(0.0f, 0.0f, 0.0f, 1.0f);
        mRS.scriptCSetScript(getResources(), R.raw.fountain);
        mRS.scriptCSetRoot(true);
        mScript = mRS.scriptCCreate();

        mScript.bindAllocation(mIntAlloc, 0);
        mScript.bindAllocation(mPartAlloc, 1);
        mScript.bindAllocation(mVertAlloc, 2);
        mRS.contextBindRootScript(mScript);

    }
    private FountainRS mRender;

    public void surfaceChanged(SurfaceHolder holder, int format, int w, int h) {
        super.surfaceChanged(holder, format, w, h);

        initRS();
    }

    @Override
    public boolean onKeyDown(int keyCode, KeyEvent event)
    {
        // break point at here
        // this method doesn't work when 'extends View' include 'extends ScrollView'.
        return super.onKeyDown(keyCode, event);
        mRS = createRenderScript();
        mRender = new FountainRS();
        mRender.init(mRS, getResources(), w, h);
    }

    int mTouchAction;

    @Override
    public boolean onTouchEvent(MotionEvent ev)
    {
        //Log.e("FountainView", ev.toString());
        boolean ret = true;
        int act = ev.getAction();
        mParams[1] = (int)ev.getX();
        mParams[2] = (int)ev.getY();

        if (act == ev.ACTION_DOWN) {
            mParams[0] = 1;
        } else if (act == ev.ACTION_UP) {
            //mParams[0] = 0;
        if (act == ev.ACTION_UP) {
            ret = false;
        }
        mIntAlloc.subData1D(2, 3, mParams);

        mRender.newTouchPosition((int)ev.getX(), (int)ev.getY());
        return ret;
    }
}
+65 −1
Original line number Diff line number Diff line
@@ -148,6 +148,15 @@ public class RenderScript {
    native private int  nProgramFragmentCreate();
    native private void nProgramFragmentDestroy(int pgm);

    native private void nProgramVertexDestroy(int pv);
    native private void nProgramVertexBindAllocation(int pv, int slot, int mID);
    native private void nProgramVertexBegin(int inID, int outID);
    native private void nProgramVertexSetType(int slot, int mID);
    native private void nProgramVertexSetCameraMode(boolean isOrtho);
    native private void nProgramVertexSetTextureMatrixEnable(boolean enable);
    native private void nProgramVertexSetModelMatrixEnable(boolean enable);
    native private int  nProgramVertexCreate();


    private int     mDev;
    private int     mContext;
@@ -229,7 +238,9 @@ public class RenderScript {
        XY_F32             (18),
        XYZ_F32            (19),
        ST_XY_F32          (20),
        ST_XYZ_F32         (21);
        ST_XYZ_F32         (21),
        NORM_XYZ_F32       (22),
        NORM_ST_XYZ_F32    (23);

        int mID;
        ElementPredefined(int id) {
@@ -680,6 +691,59 @@ public class RenderScript {
        return new Script(id);
    }

    //////////////////////////////////////////////////////////////////////////////////
    // ProgramVertex

    public class ProgramVertex extends BaseObj {
        ProgramVertex(int id) {
            mID = id;
        }

        public void destroy() {
            nProgramVertexDestroy(mID);
            mID = 0;
        }

        public void bindAllocation(int slot, Allocation va) {
            nProgramVertexBindAllocation(mID, slot, va.mID);
        }

    }

    public void programVertexBegin(Element in, Element out) {
        int inID = 0;
        int outID = 0;
        if (in != null) {
            inID = in.mID;
        }
        if (out != null) {
            outID = out.mID;
        }
        nProgramVertexBegin(inID, outID);
    }

    public void programVertexSetType(int slot, Type t) {
        nProgramVertexSetType(slot, t.mID);
    }

    public void programVertexSetCameraMode(boolean isOrtho) {
        nProgramVertexSetCameraMode(isOrtho);
    }

    public void programVertexSetTextureMatrixEnable(boolean enable) {
        nProgramVertexSetTextureMatrixEnable(enable);
    }

    public void programVertexSetModelMatrixEnable(boolean enable) {
        nProgramVertexSetModelMatrixEnable(enable);
    }

    public ProgramVertex programVertexCreate() {
        int id = nProgramVertexCreate();
        return new ProgramVertex(id);
    }


    //////////////////////////////////////////////////////////////////////////////////
    // ProgramFragmentStore

+77 −0
Original line number Diff line number Diff line
@@ -755,6 +755,74 @@ nProgramFragmentDestroy(JNIEnv *_env, jobject _this, jint pgm)
    rsProgramFragmentDestroy((RsProgramFragment)pgm);
}

// ---------------------------------------------------------------------------

static void
nProgramVertexBegin(JNIEnv *_env, jobject _this, jint in, jint out)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexBegin, con(%p), in(%p), out(%p)", con, (RsElement)in, (RsElement)out);
    rsProgramVertexBegin((RsElement)in, (RsElement)out);
}

static void
nProgramVertexBindAllocation(JNIEnv *_env, jobject _this, jint vpv, jint slot, jint a)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexBindAllocation, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsAllocation)a);
    rsProgramVertexBindAllocation((RsProgramFragment)vpv, slot, (RsAllocation)a);
}

static void
nProgramVertexSetType(JNIEnv *_env, jobject _this, jint slot, jint t)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexSetType, con(%p), vpf(%p), slot(%i), a(%p)", con, (RsProgramVertex)vpv, slot, (RsType)t);
    rsProgramVertexSetType(slot, (RsType)t);
}

static void
nProgramVertexSetCameraMode(JNIEnv *_env, jobject _this, jboolean isOrtho)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexSetCameraMode, con(%p), isOrtho(%i)", con, isOrtho);
    rsProgramVertexSetCameraMode(isOrtho);
}

static void
nProgramVertexSetTextureMatrixEnable(JNIEnv *_env, jobject _this, jboolean enable)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexSetTextureMatrixEnable, con(%p), enable(%i)", con, enable);
    rsProgramVertexSetTextureMatrixEnable(enable);
}

static void
nProgramVertexSetModelMatrixEnable(JNIEnv *_env, jobject _this, jboolean enable)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexSetModelMatrixEnable, con(%p), enable(%i)", con, enable);
    rsProgramVertexSetModelMatrixEnable(enable);
}

static jint
nProgramVertexCreate(JNIEnv *_env, jobject _this)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramVertexCreate, con(%p)", con);
    return (jint)rsProgramVertexCreate();
}

static void
nProgramVertexDestroy(JNIEnv *_env, jobject _this, jint pgm)
{
    RsContext con = (RsContext)(_env->GetIntField(_this, gContextId));
    LOG_API("nProgramFragmentDestroy, con(%p), pgm(%i)", con, pgm);
    rsProgramFragmentDestroy((RsProgramFragment)pgm);
}




// ---------------------------------------------------------------------------

@@ -902,6 +970,15 @@ static JNINativeMethod methods[] = {
{"nProgramFragmentCreate",         "()I",                                  (void*)nProgramFragmentCreate },
{"nProgramFragmentDestroy",        "(I)V",                                 (void*)nProgramFragmentDestroy },

{"nProgramVertexDestroy",          "(I)V",                                 (void*)nProgramVertexDestroy },
{"nProgramVertexBindAllocation",   "(III)V",                               (void*)nProgramVertexBindAllocation },
{"nProgramVertexBegin",            "(II)V",                                (void*)nProgramVertexBegin },
{"nProgramVertexSetType",          "(II)V",                                (void*)nProgramVertexSetType },
{"nProgramVertexSetCameraMode",    "(Z)V",                                 (void*)nProgramVertexSetCameraMode },
{"nProgramVertexSetTextureMatrixEnable",   "(Z)V",                         (void*)nProgramVertexSetTextureMatrixEnable },
{"nProgramVertexSetModelMatrixEnable",  "(Z)V",                            (void*)nProgramVertexSetModelMatrixEnable },
{"nProgramVertexCreate",           "()I",                                  (void*)nProgramVertexCreate },

{"nContextBindRootScript",         "(I)V",                                 (void*)nContextBindRootScript },
{"nContextBindProgramFragmentStore","(I)V",                                (void*)nContextBindProgramFragmentStore },
{"nContextBindProgramFragment",    "(I)V",                                 (void*)nContextBindProgramFragment },