Home | History | Annotate | Download | only in scenegraph
      1 /*
      2  * Copyright (C) 2011 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 com.android.scenegraph;
     18 
     19 import java.lang.Math;
     20 import java.util.ArrayList;
     21 import android.content.res.Resources;
     22 
     23 import android.renderscript.Allocation;
     24 import android.renderscript.Element;
     25 import android.renderscript.Matrix4f;
     26 import android.renderscript.ProgramFragment;
     27 import android.renderscript.ProgramRaster;
     28 import android.renderscript.ProgramStore;
     29 import android.renderscript.ProgramVertex;
     30 import android.renderscript.RSRuntimeException;
     31 import android.renderscript.RenderScript;
     32 import android.renderscript.RenderScriptGL;
     33 import android.util.Log;
     34 
     35 /**
     36  * @hide
     37  */
     38 public class RenderState extends SceneGraphBase {
     39     VertexShader mVertex;
     40     FragmentShader mFragment;
     41     ProgramStore mStore;
     42     ProgramRaster mRaster;
     43 
     44     ScriptField_RenderState_s mField;
     45 
     46     public RenderState(VertexShader pv,
     47                        FragmentShader pf,
     48                        ProgramStore ps,
     49                        ProgramRaster pr) {
     50         mVertex = pv;
     51         mFragment = pf;
     52         mStore = ps;
     53         mRaster = pr;
     54     }
     55 
     56     public RenderState(RenderState r) {
     57         mVertex = r.mVertex;
     58         mFragment = r.mFragment;
     59         mStore = r.mStore;
     60         mRaster = r.mRaster;
     61     }
     62 
     63     public void setProgramVertex(VertexShader pv) {
     64         mVertex = pv;
     65         updateRSData();
     66     }
     67 
     68     public void setProgramFragment(FragmentShader pf) {
     69         mFragment = pf;
     70         updateRSData();
     71     }
     72 
     73     public void setProgramStore(ProgramStore ps) {
     74         mStore = ps;
     75         updateRSData();
     76     }
     77 
     78     public void setProgramRaster(ProgramRaster pr) {
     79         mRaster = pr;
     80         updateRSData();
     81     }
     82 
     83     void updateRSData() {
     84         if (mField == null) {
     85             return;
     86         }
     87         ScriptField_RenderState_s.Item item = new ScriptField_RenderState_s.Item();
     88         item.pv = mVertex.getRSData().getAllocation();
     89         item.pf = mFragment.getRSData().getAllocation();
     90         item.ps = mStore;
     91         item.pr = mRaster;
     92 
     93         mField.set(item, 0, true);
     94     }
     95 
     96     public ScriptField_RenderState_s getRSData() {
     97         if (mField != null) {
     98             return mField;
     99         }
    100 
    101         RenderScriptGL rs = SceneManager.getRS();
    102         if (rs == null) {
    103             return null;
    104         }
    105 
    106         mField = new ScriptField_RenderState_s(rs, 1);
    107         updateRSData();
    108 
    109         return mField;
    110     }
    111 }
    112