Home | History | Annotate | Download | only in renderscript
      1 /*
      2  * Copyright (C) 2008 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package android.renderscript;
     18 
     19 /**
     20  *
     21  **/
     22 public class Script extends BaseObj {
     23     /**
     24      * Only intended for use by generated reflected code.
     25      *
     26      * @param slot
     27      */
     28     protected void invoke(int slot) {
     29         mRS.nScriptInvoke(getID(), slot);
     30     }
     31 
     32     /**
     33      * Only intended for use by generated reflected code.
     34      *
     35      * @param slot
     36      * @param v
     37      */
     38     protected void invoke(int slot, FieldPacker v) {
     39         if (v != null) {
     40             mRS.nScriptInvokeV(getID(), slot, v.getData());
     41         } else {
     42             mRS.nScriptInvoke(getID(), slot);
     43         }
     44     }
     45 
     46     /**
     47      * Only intended for use by generated reflected code.
     48      *
     49      * @param slot
     50      * @param ain
     51      * @param aout
     52      * @param v
     53      */
     54     protected void forEach(int slot, Allocation ain, Allocation aout, FieldPacker v) {
     55         if (ain == null && aout == null) {
     56             throw new RSIllegalArgumentException(
     57                 "At least one of ain or aout is required to be non-null.");
     58         }
     59         int in_id = 0;
     60         if (ain != null) {
     61             in_id = ain.getID();
     62         }
     63         int out_id = 0;
     64         if (aout != null) {
     65             out_id = aout.getID();
     66         }
     67         byte[] params = null;
     68         if (v != null) {
     69             params = v.getData();
     70         }
     71         mRS.nScriptForEach(getID(), slot, in_id, out_id, params);
     72     }
     73 
     74 
     75     Script(int id, RenderScript rs) {
     76         super(id, rs);
     77     }
     78 
     79 
     80     /**
     81      * Only intended for use by generated reflected code.
     82      *
     83      * @param va
     84      * @param slot
     85      */
     86     public void bindAllocation(Allocation va, int slot) {
     87         mRS.validate();
     88         if (va != null) {
     89             mRS.nScriptBindAllocation(getID(), va.getID(), slot);
     90         } else {
     91             mRS.nScriptBindAllocation(getID(), 0, slot);
     92         }
     93     }
     94 
     95     /**
     96      * Only intended for use by generated reflected code.
     97      *
     98      * @param index
     99      * @param v
    100      */
    101     public void setVar(int index, float v) {
    102         mRS.nScriptSetVarF(getID(), index, v);
    103     }
    104 
    105     /**
    106      * Only intended for use by generated reflected code.
    107      *
    108      * @param index
    109      * @param v
    110      */
    111     public void setVar(int index, double v) {
    112         mRS.nScriptSetVarD(getID(), index, v);
    113     }
    114 
    115     /**
    116      * Only intended for use by generated reflected code.
    117      *
    118      * @param index
    119      * @param v
    120      */
    121     public void setVar(int index, int v) {
    122         mRS.nScriptSetVarI(getID(), index, v);
    123     }
    124 
    125     /**
    126      * Only intended for use by generated reflected code.
    127      *
    128      * @param index
    129      * @param v
    130      */
    131     public void setVar(int index, long v) {
    132         mRS.nScriptSetVarJ(getID(), index, v);
    133     }
    134 
    135     /**
    136      * Only intended for use by generated reflected code.
    137      *
    138      * @param index
    139      * @param v
    140      */
    141     public void setVar(int index, boolean v) {
    142         mRS.nScriptSetVarI(getID(), index, v ? 1 : 0);
    143     }
    144 
    145     /**
    146      * Only intended for use by generated reflected code.
    147      *
    148      * @param index
    149      * @param o
    150      */
    151     public void setVar(int index, BaseObj o) {
    152         mRS.nScriptSetVarObj(getID(), index, (o == null) ? 0 : o.getID());
    153     }
    154 
    155     /**
    156      * Only intended for use by generated reflected code.
    157      *
    158      * @param index
    159      * @param v
    160      */
    161     public void setVar(int index, FieldPacker v) {
    162         mRS.nScriptSetVarV(getID(), index, v.getData());
    163     }
    164 
    165     public void setTimeZone(String timeZone) {
    166         mRS.validate();
    167         try {
    168             mRS.nScriptSetTimeZone(getID(), timeZone.getBytes("UTF-8"));
    169         } catch (java.io.UnsupportedEncodingException e) {
    170             throw new RuntimeException(e);
    171         }
    172     }
    173 
    174     public static class Builder {
    175         RenderScript mRS;
    176 
    177         Builder(RenderScript rs) {
    178             mRS = rs;
    179         }
    180     }
    181 
    182 
    183     public static class FieldBase {
    184         protected Element mElement;
    185         protected Allocation mAllocation;
    186 
    187         protected void init(RenderScript rs, int dimx) {
    188             mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT);
    189         }
    190 
    191         protected void init(RenderScript rs, int dimx, int usages) {
    192             mAllocation = Allocation.createSized(rs, mElement, dimx, Allocation.USAGE_SCRIPT | usages);
    193         }
    194 
    195         protected FieldBase() {
    196         }
    197 
    198         public Element getElement() {
    199             return mElement;
    200         }
    201 
    202         public Type getType() {
    203             return mAllocation.getType();
    204         }
    205 
    206         public Allocation getAllocation() {
    207             return mAllocation;
    208         }
    209 
    210         //@Override
    211         public void updateAllocation() {
    212         }
    213     }
    214 }
    215 
    216