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  * @hide
     21  **/
     22 public class Script extends BaseObj {
     23     public static final int MAX_SLOT = 16;
     24 
     25     boolean mIsRoot;
     26     Type[] mTypes;
     27     boolean[] mWritable;
     28     Invokable[] mInvokables;
     29 
     30     public static class Invokable {
     31         RenderScript mRS;
     32         Script mScript;
     33         int mSlot;
     34         String mName;
     35 
     36         Invokable() {
     37             mSlot = -1;
     38         }
     39 
     40         public void execute() {
     41             mRS.nScriptInvoke(mScript.mID, mSlot);
     42         }
     43     }
     44 
     45     Script(int id, RenderScript rs) {
     46         super(rs);
     47         mID = id;
     48     }
     49 
     50     public void bindAllocation(Allocation va, int slot) {
     51         mRS.validate();
     52         mRS.nScriptBindAllocation(mID, va.mID, slot);
     53     }
     54 
     55     public void setClearColor(float r, float g, float b, float a) {
     56         mRS.validate();
     57         mRS.nScriptSetClearColor(mID, r, g, b, a);
     58     }
     59 
     60     public void setClearDepth(float d) {
     61         mRS.validate();
     62         mRS.nScriptSetClearDepth(mID, d);
     63     }
     64 
     65     public void setClearStencil(int stencil) {
     66         mRS.validate();
     67         mRS.nScriptSetClearStencil(mID, stencil);
     68     }
     69 
     70     public void setTimeZone(String timeZone) {
     71         mRS.validate();
     72         try {
     73             mRS.nScriptSetTimeZone(mID, timeZone.getBytes("UTF-8"));
     74         } catch (java.io.UnsupportedEncodingException e) {
     75             throw new RuntimeException(e);
     76         }
     77     }
     78 
     79     public static class Builder {
     80         RenderScript mRS;
     81         boolean mIsRoot = false;
     82         Type[] mTypes;
     83         String[] mNames;
     84         boolean[] mWritable;
     85         int mInvokableCount = 0;
     86         Invokable[] mInvokables;
     87 
     88         Builder(RenderScript rs) {
     89             mRS = rs;
     90             mTypes = new Type[MAX_SLOT];
     91             mNames = new String[MAX_SLOT];
     92             mWritable = new boolean[MAX_SLOT];
     93             mInvokables = new Invokable[MAX_SLOT];
     94         }
     95 
     96         public void setType(Type t, int slot) {
     97             mTypes[slot] = t;
     98             mNames[slot] = null;
     99         }
    100 
    101         public void setType(Type t, String name, int slot) {
    102             mTypes[slot] = t;
    103             mNames[slot] = name;
    104         }
    105 
    106         public Invokable addInvokable(String func) {
    107             Invokable i = new Invokable();
    108             i.mName = func;
    109             i.mRS = mRS;
    110             i.mSlot = mInvokableCount;
    111             mInvokables[mInvokableCount++] = i;
    112             return i;
    113         }
    114 
    115         public void setType(boolean writable, int slot) {
    116             mWritable[slot] = writable;
    117         }
    118 
    119         void transferCreate() {
    120             mRS.nScriptSetRoot(mIsRoot);
    121             for(int ct=0; ct < mTypes.length; ct++) {
    122                 if(mTypes[ct] != null) {
    123                     mRS.nScriptSetType(mTypes[ct].mID, mWritable[ct], mNames[ct], ct);
    124                 }
    125             }
    126             for(int ct=0; ct < mInvokableCount; ct++) {
    127                 mRS.nScriptSetInvokable(mInvokables[ct].mName, ct);
    128             }
    129         }
    130 
    131         void transferObject(Script s) {
    132             s.mIsRoot = mIsRoot;
    133             s.mTypes = mTypes;
    134             s.mInvokables = new Invokable[mInvokableCount];
    135             for(int ct=0; ct < mInvokableCount; ct++) {
    136                 s.mInvokables[ct] = mInvokables[ct];
    137                 s.mInvokables[ct].mScript = s;
    138             }
    139             s.mInvokables = null;
    140         }
    141 
    142         public void setRoot(boolean r) {
    143             mIsRoot = r;
    144         }
    145 
    146     }
    147 
    148 }
    149 
    150