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

Commit 36e612a4 authored by Jason Sams's avatar Jason Sams
Browse files

Begin splitting up RenderScript.java into seperate classes. First piece split off Element.

parent b5a57ad9
Loading
Loading
Loading
Loading
+67 −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.Log;

/**
 * @hide
 *
 **/
class BaseObj {

    BaseObj(RenderScript rs) {
        mRS = rs;
        mID = 0;
    }

    public int getID() {
        return mID;
    }

    int mID;
    String mName;
    RenderScript mRS;

    public void setName(String s) throws IllegalStateException, IllegalArgumentException
    {
        if(s.length() < 1) {
            throw new IllegalArgumentException("setName does not accept a zero length string.");
        }
        if(mName != null) {
            throw new IllegalArgumentException("setName object already has a name.");
        }

        try {
            byte[] bytes = s.getBytes("UTF-8");
            mRS.nAssignName(mID, bytes);
            mName = s;
        } catch (java.io.UnsupportedEncodingException e) {
            throw new RuntimeException(e);
        }
    }

    protected void finalize() throws Throwable
    {
        if (mID != 0) {
            Log.v(RenderScript.LOG_TAG,
                  "Element finalized without having released the RS reference.");
        }
        super.finalize();
    }
}
+205 −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;


/**
 * @hide
 *
 **/
public class Element extends BaseObj {
    final int mPredefinedID;
    final boolean mIsPredefined;

    public static final Element USER_U8 = new Element(0);
    public static final Element USER_I8 = new Element(1);
    public static final Element USER_U16 = new Element(2);
    public static final Element USER_I16 = new Element(3);
    public static final Element USER_U32 = new Element(4);
    public static final Element USER_I32 = new Element(5);
    public static final Element USER_FLOAT = new Element(6);

    public static final Element A_8 = new Element(7);
    public static final Element RGB_565 = new Element(8);
    public static final Element RGB_888 = new Element(11);
    public static final Element RGBA_5551 = new Element(9);
    public static final Element RGBA_4444 = new Element(10);
    public static final Element RGBA_8888 = new Element(12);

    public static final Element INDEX_16 = new Element(13);
    public static final Element INDEX_32 = new Element(14);
    public static final Element XY_F32 = new Element(15);
    public static final Element XYZ_F32 = new Element(16);
    public static final Element ST_XY_F32 = new Element(17);
    public static final Element ST_XYZ_F32 = new Element(18);
    public static final Element NORM_XYZ_F32 = new Element(19);
    public static final Element NORM_ST_XYZ_F32 = new Element(20);

    void initPredef(RenderScript rs) {
        mID = rs.nElementGetPredefined(mPredefinedID);
    }

    static void init(RenderScript rs) {
        USER_U8.initPredef(rs);
        USER_I8.initPredef(rs);
        USER_U16.initPredef(rs);
        USER_I16.initPredef(rs);
        USER_U32.initPredef(rs);
        USER_I32.initPredef(rs);
        USER_FLOAT.initPredef(rs);

        A_8.initPredef(rs);
        RGB_565.initPredef(rs);
        RGB_888.initPredef(rs);
        RGBA_5551.initPredef(rs);
        RGBA_4444.initPredef(rs);
        RGBA_8888.initPredef(rs);

        INDEX_16.initPredef(rs);
        INDEX_32.initPredef(rs);
        XY_F32.initPredef(rs);
        XYZ_F32.initPredef(rs);
        ST_XY_F32.initPredef(rs);
        ST_XYZ_F32.initPredef(rs);
        NORM_XYZ_F32.initPredef(rs);
        NORM_ST_XYZ_F32.initPredef(rs);
    }


    public enum DataType {
        FLOAT (0),
        UNSIGNED (1),
        SIGNED (2);

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

    public enum DataKind {
        USER (0),
        RED (1),
        GREEN (2),
        BLUE (3),
        ALPHA (4),
        LUMINANCE (5),
        INTENSITY (6),
        X (7),
        Y (8),
        Z (9),
        W (10),
        S (11),
        T (12),
        Q (13),
        R (14),
        NX (15),
        NY (16),
        NZ (17),
        INDEX (18);

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


    Element(int predef) {
        super(null);
        mID = 0;
        mPredefinedID = predef;
        mIsPredefined = true;
    }

    Element(int id, RenderScript rs) {
        super(rs);
        mID = id;
        mPredefinedID = 0;
        mIsPredefined = false;
    }

    public void destroy() throws IllegalStateException {
        if(mIsPredefined) {
            throw new IllegalStateException("Attempting to destroy a predefined Element.");
        }
        mRS.nElementDestroy(mID);
        mID = 0;
    }




    public static class Builder {
        RenderScript mRS;
        boolean mActive = true;

        Builder(RenderScript rs) {
            mRS = rs;
        }

        void begin() throws IllegalStateException {
            if (mActive) {
                throw new IllegalStateException("Element builder already active.");
            }
            mRS.nElementBegin();
            mActive = true;
        }

        public Builder add(Element e) throws IllegalArgumentException, IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
            }
            if(!e.mIsPredefined) {
                throw new IllegalArgumentException("add requires a predefined Element.");
            }
            mRS.nElementAddPredefined(e.mID);
            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.");
            }
            int norm = 0;
            if (isNormalized) {
                norm = 1;
            }
            mRS.nElementAdd(dt.mID, dk.mID, norm, bits);
            return this;
        }

        public void abort() throws IllegalStateException {
            if(!mActive) {
                throw new IllegalStateException("Element builder not active.");
            }
            mActive = false;
        }

        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);
        }
    }

}
+1 −4
Original line number Diff line number Diff line
@@ -40,10 +40,7 @@ public class ProgramVertexAlloc {
        mProjection = new Matrix();
        mTexture = new Matrix();

        mAlloc = rs.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_FLOAT, 
            48);

        mAlloc = rs.allocationCreateSized(Element.USER_FLOAT, 48);
        mAlloc.subData1D(MODELVIEW_OFFSET, 16, mModel.mMat);
        mAlloc.subData1D(PROJECTION_OFFSET, 16, mProjection.mMat);
        mAlloc.subData1D(TEXTURE_OFFSET, 16, mTexture.mMat);
+168 −285

File changed.

Preview size limit exceeded, changes collapsed.

+18 −37
Original line number Diff line number Diff line
@@ -21,22 +21,12 @@ import java.io.Writer;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.os.Message;
import android.util.Log;

import android.renderscript.Matrix;
import android.renderscript.ProgramVertexAlloc;
import android.renderscript.RenderScript;
import android.renderscript.RenderScript.ElementPredefined;
import android.util.AttributeSet;
import android.util.Log;
import android.view.KeyEvent;
import android.view.MotionEvent;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import android.renderscript.Element;

public class FilmRS {
    private final int POS_TRANSLATE = 0;
@@ -76,8 +66,8 @@ public class FilmRS {
    private RenderScript mRS;
    private RenderScript.Script mScriptStrip;
    private RenderScript.Script mScriptImage;
    private RenderScript.Element mElementVertex;
    private RenderScript.Element mElementIndex;
    private Element mElementVertex;
    private Element mElementIndex;
    private RenderScript.Sampler mSampler;
    private RenderScript.ProgramFragmentStore mPFSBackground;
    private RenderScript.ProgramFragmentStore mPFSImages;
@@ -166,13 +156,10 @@ public class FilmRS {
    private void loadImages() {
        mBufferIDs = new int[13];
        mImages = new RenderScript.Allocation[13];
        mAllocIDs = mRS.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_FLOAT,
            mBufferIDs.length);

        RenderScript.ElementPredefined ie = 
            RenderScript.ElementPredefined.RGB_565;
        mAllocIDs = mRS.allocationCreateSized(
            Element.USER_FLOAT, mBufferIDs.length);

        Element ie = Element.RGB_565;
        mImages[0] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p01, ie, true);
        mImages[1] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p02, ie, true);
        mImages[2] = mRS.allocationCreateFromBitmapResourceBoxed(mRes, R.drawable.p03, ie, true);
@@ -197,9 +184,8 @@ public class FilmRS {
    private void initState()
    {
        mBufferState = new int[10];
        mAllocState = mRS.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_FLOAT,
            mBufferState.length);
        mAllocState = mRS.allocationCreateSized(
            Element.USER_FLOAT, mBufferState.length);

        mBufferState[STATE_TRIANGLE_OFFSET_COUNT] = mFSM.mTriangleOffsetsCount;
        mBufferState[STATE_LAST_FOCUS] = -1;
@@ -208,10 +194,8 @@ public class FilmRS {
    }

    private void initRS() {
        mElementVertex = mRS.elementGetPredefined(
            RenderScript.ElementPredefined.NORM_ST_XYZ_F32);
        mElementIndex = mRS.elementGetPredefined(
            RenderScript.ElementPredefined.INDEX_16);
        mElementVertex = Element.NORM_ST_XYZ_F32;
        mElementIndex = Element.INDEX_16;

        mRS.triangleMeshBegin(mElementVertex, mElementIndex);
        mFSM = new FilmStripMesh();
@@ -231,9 +215,8 @@ public class FilmRS {
        mRS.scriptCSetRoot(true);
        mScriptStrip = mRS.scriptCCreate();

        mAllocPos = mRS.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_FLOAT,
            mBufferPos.length);
        mAllocPos = mRS.allocationCreateSized(
            Element.USER_FLOAT, mBufferPos.length);

        loadImages();
        initState();
@@ -250,15 +233,13 @@ public class FilmRS {
        mScriptStrip.bindAllocation(mPVA.mAlloc, 3);


        mAllocOffsets = mRS.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_I32,
            mFSM.mTriangleOffsets.length);
        mAllocOffsets = mRS.allocationCreateSized(
            Element.USER_I32, mFSM.mTriangleOffsets.length);
        mAllocOffsets.data(mFSM.mTriangleOffsets);
        mScriptStrip.bindAllocation(mAllocOffsets, 4);

        mAllocOffsetsTex = mRS.allocationCreatePredefSized(
            RenderScript.ElementPredefined.USER_FLOAT,
            mFSM.mTriangleOffsetsTex.length);
        mAllocOffsetsTex = mRS.allocationCreateSized(
            Element.USER_FLOAT, mFSM.mTriangleOffsetsTex.length);
        mAllocOffsetsTex.data(mFSM.mTriangleOffsetsTex);
        mScriptStrip.bindAllocation(mAllocOffsetsTex, 5);

Loading