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

Commit 22534176 authored by Jason Sams's avatar Jason Sams
Browse files

Split ProgramFragment and ProgramStore from RenderScript.java. Update Element...

Split ProgramFragment and ProgramStore from RenderScript.java.  Update Element and Type to new cached builder for easier app developement.
parent 959b7bd9
Loading
Loading
Loading
Loading
+50 −33
Original line number Diff line number Diff line
@@ -146,58 +146,75 @@ public class Element extends BaseObj {

    public static class Builder {
        RenderScript mRS;
        boolean mActive = true;
        Entry[] mEntries;
        int mEntryCount;

        Builder(RenderScript rs) {
        private class Entry {
            Element mElement;
            Element.DataType mType;
            Element.DataKind mKind;
            boolean mIsNormalized;
            int mBits;
        }

        public Builder(RenderScript rs) {
            mRS = rs;
            mEntryCount = 0;
            mEntries = new Entry[8];
        }

        void begin() throws IllegalStateException {
            if (mActive) {
                throw new IllegalStateException("Element builder already active.");
        void addEntry(Entry e) {
            if(mEntries.length >= mEntryCount) {
                Entry[] en = new Entry[mEntryCount + 8];
                for(int ct=0; ct < mEntries.length; ct++) {
                    en[ct] = mEntries[ct];
                }
            mRS.nElementBegin();
            mActive = true;
                mEntries = en;
            }

        public Builder add(Element e) throws IllegalArgumentException, IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
            mEntries[mEntryCount] = e;
            mEntryCount++;
        }

        public Builder add(Element e) throws IllegalArgumentException {
            if(!e.mIsPredefined) {
                throw new IllegalArgumentException("add requires a predefined Element.");
            }
            mRS.nElementAddPredefined(e.mID);
            Entry en = new Entry();
            en.mElement = e;
            addEntry(en);
            return this;
        }

        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits)
            throws IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
        public Builder add(Element.DataType dt, Element.DataKind dk, boolean isNormalized, int bits) {
            Entry en = new Entry();
            en.mType = dt;
            en.mKind = dk;
            en.mIsNormalized = isNormalized;
            en.mBits = bits;
            addEntry(en);
            return this;
        }

        static synchronized Element internalCreate(RenderScript rs, Builder b) {
            rs.nElementBegin();
            for (int ct=0; ct < b.mEntryCount; ct++) {
                Entry en = b.mEntries[ct];
                if(en.mElement !=  null) {
                    rs.nElementAddPredefined(en.mElement.mPredefinedID);
                } else {
                    int norm = 0;
            if (isNormalized) {
                    if (en.mIsNormalized) {
                        norm = 1;
                    }
            mRS.nElementAdd(dt.mID, dk.mID, norm, bits);
            return this;
                    rs.nElementAdd(en.mType.mID, en.mKind.mID, norm, en.mBits);
                }

        public void abort() throws IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
            }
            mActive = false;
            int id = rs.nElementCreate();
            return new Element(id, rs);
        }

        public Element create() throws IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
            }
            int id = mRS.nElementCreate();
            mActive = false;
            return new Element(id, mRS);
        public Element create() {
            return internalCreate(mRS, this);
        }
    }

+150 −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 android.renderscript;


import android.util.Config;
import android.util.Log;


/**
 * @hide
 *
 **/
public class ProgramFragment extends BaseObj {
    public enum EnvMode {
        REPLACE (0),
        MODULATE (1),
        DECAL (2);

        int mID;
        EnvMode(int id) {
            mID = id;
        }
    }


    ProgramFragment(int id, RenderScript rs) {
        super(rs);
        mID = id;
    }

    public void destroy() {
        mRS.nProgramFragmentStoreDestroy(mID);
        mID = 0;
    }

    public void bindTexture(Allocation va, int slot) {
        mRS.nProgramFragmentBindTexture(mID, slot, va.mID);
    }

    public void bindSampler(RenderScript.Sampler vs, int slot) {
        mRS.nProgramFragmentBindSampler(mID, slot, vs.mID);
    }


    public static class Builder {
        public static final int MAX_SLOT = 2;
        RenderScript mRS;
        Element mIn;
        Element mOut;

        private class Slot {
            Type mType;
            EnvMode mEnv;
            boolean mTexEnable;

            Slot() {
                mTexEnable = false;
            }
        }
        Slot[] mSlots;

        public Builder(RenderScript rs, Element in, Element out) {
            mRS = rs;
            mIn = in;
            mOut = out;
            mSlots = new Slot[MAX_SLOT];
            for(int ct=0; ct < MAX_SLOT; ct++) {
                mSlots[ct] = new Slot();
            }
        }

        public void setType(int slot, Type t)
            throws IllegalArgumentException {
            if((slot < 0) || (slot >= MAX_SLOT)) {
                throw new IllegalArgumentException("Slot ID out of range.");
            }

            mSlots[slot].mType = t;
        }

        public void setTexEnable(boolean enable, int slot)
            throws IllegalArgumentException {
            if((slot < 0) || (slot >= MAX_SLOT)) {
                throw new IllegalArgumentException("Slot ID out of range.");
            }

            mSlots[slot].mTexEnable = enable;
        }

        public void setTexEnvMode(EnvMode env, int slot)
            throws IllegalArgumentException {
            if((slot < 0) || (slot >= MAX_SLOT)) {
                throw new IllegalArgumentException("Slot ID out of range.");
            }

            mSlots[slot].mEnv = env;
        }


        static synchronized ProgramFragment internalCreate(RenderScript rs, Builder b) {
            int inID = 0;
            int outID = 0;
            if (b.mIn != null) {
                inID = b.mIn.mID;
            }
            if (b.mOut != null) {
                outID = b.mOut.mID;
            }
            rs.nProgramFragmentBegin(inID, outID);
            for(int ct=0; ct < MAX_SLOT; ct++) {
                if(b.mSlots[ct].mTexEnable) {
                    Slot s = b.mSlots[ct];
                    if(s.mType != null) {
                        rs.nProgramFragmentSetType(ct, s.mType.mID);
                    }
                    rs.nProgramFragmentSetTexEnable(ct, true);
                    if(s.mEnv != null) {
                        rs.nProgramFragmentSetEnvMode(ct, s.mEnv.mID);
                    }
                }
            }


            int id = rs.nProgramFragmentCreate();
            return new ProgramFragment(id, rs);
        }

        public ProgramFragment create() {
            return internalCreate(mRS, this);
        }
    }
}


+178 −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 android.renderscript;


import android.util.Config;
import android.util.Log;


/**
 * @hide
 *
 **/
public class ProgramStore extends BaseObj {
        public enum DepthFunc {
        ALWAYS (0),
        LESS (1),
        LEQUAL (2),
        GREATER (3),
        GEQUAL (4),
        EQUAL (5),
        NOTEQUAL (6);

        int mID;
        DepthFunc(int id) {
            mID = id;
        }
    }

    public enum BlendSrcFunc {
        ZERO (0),
        ONE (1),
        DST_COLOR (2),
        ONE_MINUS_DST_COLOR (3),
        SRC_ALPHA (4),
        ONE_MINUS_SRC_ALPHA (5),
        DST_ALPHA (6),
        ONE_MINUS_DST_ALPA (7),
        SRC_ALPHA_SATURATE (8);

        int mID;
        BlendSrcFunc(int id) {
            mID = id;
        }
    }

    public enum BlendDstFunc {
        ZERO (0),
        ONE (1),
        SRC_COLOR (2),
        ONE_MINUS_SRC_COLOR (3),
        SRC_ALPHA (4),
        ONE_MINUS_SRC_ALPHA (5),
        DST_ALPHA (6),
        ONE_MINUS_DST_ALPA (7);

        int mID;
        BlendDstFunc(int id) {
            mID = id;
        }
    }


    ProgramStore(int id, RenderScript rs) {
        super(rs);
        mID = id;
    }

    public void destroy() {
        mRS.nProgramFragmentStoreDestroy(mID);
        mID = 0;
    }



    public static class Builder {
        RenderScript mRS;
        Element mIn;
        Element mOut;
        DepthFunc mDepthFunc;
        boolean mDepthMask;
        boolean mColorMaskR;
        boolean mColorMaskG;
        boolean mColorMaskB;
        boolean mColorMaskA;
        BlendSrcFunc mBlendSrc;
        BlendDstFunc mBlendDst;
        boolean mDither;



        public Builder(RenderScript rs, Element in, Element out) {
            mRS = rs;
            mIn = in;
            mOut = out;
            mDepthFunc = DepthFunc.ALWAYS;
            mDepthMask = false;
            mColorMaskR = true;
            mColorMaskG = true;
            mColorMaskB = true;
            mColorMaskA = true;
            mBlendSrc = BlendSrcFunc.ONE;
            mBlendDst = BlendDstFunc.ZERO;


        }

        public void setDepthFunc(DepthFunc func) {
            mDepthFunc = func;
        }

        public void setDepthMask(boolean enable) {
            mDepthMask = enable;
        }

        public void setColorMask(boolean r, boolean g, boolean b, boolean a) {
            mColorMaskR = r;
            mColorMaskG = g;
            mColorMaskB = b;
            mColorMaskA = a;
        }

        public void setBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
            mBlendSrc = src;
            mBlendDst = dst;
        }

        public void setDitherEnable(boolean enable) {
            mDither = enable;
        }

        static synchronized ProgramStore internalCreate(RenderScript rs, Builder b) {
            int inID = 0;
            int outID = 0;
            if (b.mIn != null) {
                inID = b.mIn.mID;
            }
            if (b.mOut != null) {
                outID = b.mOut.mID;
            }
            rs.nProgramFragmentStoreBegin(inID, outID);
            rs.nProgramFragmentStoreDepthFunc(b.mDepthFunc.mID);
            rs.nProgramFragmentStoreDepthMask(b.mDepthMask);
            rs.nProgramFragmentStoreColorMask(b.mColorMaskR,
                                              b.mColorMaskG,
                                              b.mColorMaskB,
                                              b.mColorMaskA);
            rs.nProgramFragmentStoreBlendFunc(b.mBlendSrc.mID, b.mBlendDst.mID);
            rs.nProgramFragmentStoreDither(b.mDither);

            int id = rs.nProgramFragmentStoreCreate();
            return new ProgramStore(id, rs);
        }

        public ProgramStore create() {
            return internalCreate(mRS, this);
        }
    }

}



+7 −179
Original line number Diff line number Diff line
@@ -128,12 +128,13 @@ public class RenderScript {
    native int  nAdapter2DCreate();

    native void nScriptDestroy(int script);
    native void nScriptBindAllocation(int vtm, int alloc, int slot);
    native void nScriptBindAllocation(int script, int alloc, int slot);
    native void nScriptSetClearColor(int script, float r, float g, float b, float a);
    native void nScriptSetClearDepth(int script, float depth);
    native void nScriptSetClearStencil(int script, int stencil);
    native void nScriptSetTimeZone(int script, byte[] timeZone);

    native void nScriptCBegin();
    native void nScriptCSetClearColor(float r, float g, float b, float a);
    native void nScriptCSetClearDepth(float depth);
    native void nScriptCSetClearStencil(int stencil);
    native void nScriptCSetTimeZone(byte[] timeZone);
    native void nScriptCAddType(int type);
    native void nScriptCSetRoot(boolean isRoot);
    native void nScriptCSetScript(byte[] script, int offset, int length);
@@ -203,77 +204,6 @@ public class RenderScript {
    //////////////////////////////////////////////////////////////////////////////////
    // Element

    Element.Builder mElementBuilder = new Element.Builder(this);
    public Element.Builder elementBuilderCreate() throws IllegalStateException {
        mElementBuilder.begin();
        return mElementBuilder;
    }

    Type.Builder mTypeBuilder = new Type.Builder(this);
    public Type.Builder typeBuilderCreate(Element e) throws IllegalStateException {
        mTypeBuilder.begin(e);
        return mTypeBuilder;
    }


    public enum DepthFunc {
        ALWAYS (0),
        LESS (1),
        LEQUAL (2),
        GREATER (3),
        GEQUAL (4),
        EQUAL (5),
        NOTEQUAL (6);

        int mID;
        DepthFunc(int id) {
            mID = id;
        }
    }

    public enum BlendSrcFunc {
        ZERO (0),
        ONE (1),
        DST_COLOR (2),
        ONE_MINUS_DST_COLOR (3),
        SRC_ALPHA (4),
        ONE_MINUS_SRC_ALPHA (5),
        DST_ALPHA (6),
        ONE_MINUS_DST_ALPA (7),
        SRC_ALPHA_SATURATE (8);

        int mID;
        BlendSrcFunc(int id) {
            mID = id;
        }
    }

    public enum BlendDstFunc {
        ZERO (0),
        ONE (1),
        SRC_COLOR (2),
        ONE_MINUS_SRC_COLOR (3),
        SRC_ALPHA (4),
        ONE_MINUS_SRC_ALPHA (5),
        DST_ALPHA (6),
        ONE_MINUS_DST_ALPA (7);

        int mID;
        BlendDstFunc(int id) {
            mID = id;
        }
    }

    public enum EnvMode {
        REPLACE (0),
        MODULATE (1),
        DECAL (2);

        int mID;
        EnvMode(int id) {
            mID = id;
        }
    }

    public enum SamplerParam {
        FILTER_MIN (0),
@@ -402,111 +332,9 @@ public class RenderScript {
    //////////////////////////////////////////////////////////////////////////////////
    // ProgramFragmentStore

    public class ProgramFragmentStore extends BaseObj {
        ProgramFragmentStore(int id) {
            super(RenderScript.this);
            mID = id;
        }

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

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

    public void programFragmentStoreDepthFunc(DepthFunc func) {
        nProgramFragmentStoreDepthFunc(func.mID);
    }

    public void programFragmentStoreDepthMask(boolean enable) {
        nProgramFragmentStoreDepthMask(enable);
    }

    public void programFragmentStoreColorMask(boolean r, boolean g, boolean b, boolean a) {
        nProgramFragmentStoreColorMask(r,g,b,a);
    }

    public void programFragmentStoreBlendFunc(BlendSrcFunc src, BlendDstFunc dst) {
        nProgramFragmentStoreBlendFunc(src.mID, dst.mID);
    }

    public void programFragmentStoreDitherEnable(boolean enable) {
        nProgramFragmentStoreDither(enable);
    }

    public ProgramFragmentStore programFragmentStoreCreate() {
        int id = nProgramFragmentStoreCreate();
        return new ProgramFragmentStore(id);
    }

    //////////////////////////////////////////////////////////////////////////////////
    // ProgramFragment

    public class ProgramFragment extends BaseObj {
        ProgramFragment(int id) {
            super(RenderScript.this);
            mID = id;
        }

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

        public void bindTexture(Allocation va, int slot) {
            nProgramFragmentBindTexture(mID, slot, va.mID);
        }

        public void bindSampler(Sampler vs, int slot) {
            nProgramFragmentBindSampler(mID, slot, vs.mID);
        }
    }

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

    public void programFragmentSetType(int slot, Type t) {
        nProgramFragmentSetType(slot, t.mID);
    }

    public void programFragmentSetType(int slot, EnvMode t) {
        nProgramFragmentSetEnvMode(slot, t.mID);
    }

    public void programFragmentSetTexEnable(int slot, boolean enable) {
        nProgramFragmentSetTexEnable(slot, enable);
    }

    public void programFragmentSetTexEnvMode(int slot, EnvMode env) {
        nProgramFragmentSetEnvMode(slot, env.mID);
    }

    public ProgramFragment programFragmentCreate() {
        int id = nProgramFragmentCreate();
        return new ProgramFragment(id);
    }

    //////////////////////////////////////////////////////////////////////////////////
    // Sampler

@@ -617,7 +445,7 @@ public class RenderScript {
        //nContextBindSampler(s.mID);
    //}

    public void contextBindProgramFragmentStore(ProgramFragmentStore pfs) {
    public void contextBindProgramFragmentStore(ProgramStore pfs) {
        nContextBindProgramFragmentStore(pfs.mID);
    }

+10 −15
Original line number Diff line number Diff line
@@ -37,22 +37,28 @@ public class Script extends BaseObj {
    }

    public void setClearColor(float r, float g, float b, float a) {
        //mRS.nScriptCSetClearColor(r, g, b, a);
        mRS.nScriptSetClearColor(mID, r, g, b, a);
    }

    public void setClearDepth(float d) {
        //mRS.nScriptCSetClearDepth(d);
        mRS.nScriptSetClearDepth(mID, d);
    }

    public void setClearStencil(int stencil) {
        //mRS.nScriptCSetClearStencil(stencil);
        mRS.nScriptSetClearStencil(mID, stencil);
    }

    public void setTimeZone(String timeZone) {
        try {
            mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    public static class Builder {
        RenderScript mRS;
        boolean mIsRoot = false;
        byte[] mTimeZone;

        Builder(RenderScript rs) {
            mRS = rs;
@@ -63,9 +69,6 @@ public class Script extends BaseObj {
        }

        void transferCreate() {
            if(mTimeZone != null) {
                mRS.nScriptCSetTimeZone(mTimeZone);
            }
            mRS.nScriptCSetRoot(mIsRoot);
        }

@@ -73,14 +76,6 @@ public class Script extends BaseObj {
            s.mIsRoot = mIsRoot;
        }

        public void setTimeZone(String timeZone) {
            try {
                mTimeZone = timeZone.getBytes("UTF-8");
            } catch (java.io.UnsupportedEncodingException e) {
                throw new RuntimeException(e);
            }
        }

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