Home | History | Annotate | Download | only in testapp
      1 /*
      2  * Copyright (C) 2012 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.testapp;
     18 
     19 import java.util.ArrayList;
     20 import java.util.HashMap;
     21 import java.util.Map;
     22 import java.util.Vector;
     23 
     24 import com.android.scenegraph.SceneManager;
     25 
     26 import android.content.res.Resources;
     27 import android.graphics.Bitmap;
     28 import android.graphics.BitmapFactory;
     29 import android.os.AsyncTask;
     30 import android.renderscript.*;
     31 import android.renderscript.Allocation.MipmapControl;
     32 import android.renderscript.Element.Builder;
     33 import android.renderscript.Font.Style;
     34 import android.renderscript.Program.TextureType;
     35 import android.renderscript.ProgramStore.DepthFunc;
     36 import android.util.Log;
     37 
     38 // This is where the scenegraph and the rendered objects are initialized and used
     39 public class TestAppLoadingScreen {
     40 
     41     private static String TAG = "TestAppLoadingScreen";
     42 
     43     private Resources mRes;
     44     private RenderScriptGL mRS;
     45     private ScriptC_test_app mScript;
     46 
     47     public TestAppLoadingScreen(RenderScriptGL rs, Resources res) {
     48         mRS = rs;
     49         mRes = res;
     50         // Shows the loading screen with some text
     51         renderLoading();
     52         // Adds a little 3D bugdroid model to the laoding screen asynchronously.
     53         new LoadingScreenLoaderTask().execute();
     54     }
     55 
     56     public void showLoadingScreen(boolean show) {
     57         if (show) {
     58             mRS.bindRootScript(mScript);
     59         } else {
     60             mRS.bindRootScript(SceneManager.getInstance().getRenderLoop());
     61         }
     62     }
     63 
     64     // The loading screen has some elements that shouldn't be loaded on the UI thread
     65     private class LoadingScreenLoaderTask extends AsyncTask<String, Void, Boolean> {
     66         Allocation robotTex;
     67         Mesh robotMesh;
     68         protected Boolean doInBackground(String... names) {
     69             long start = System.currentTimeMillis();
     70             robotTex = Allocation.createFromBitmapResource(mRS, mRes, R.drawable.robot,
     71                                                            MipmapControl.MIPMAP_ON_SYNC_TO_TEXTURE,
     72                                                            Allocation.USAGE_GRAPHICS_TEXTURE);
     73 
     74             FileA3D model = FileA3D.createFromResource(mRS, mRes, R.raw.robot);
     75             FileA3D.IndexEntry entry = model.getIndexEntry(0);
     76             if (entry != null && entry.getEntryType() == FileA3D.EntryType.MESH) {
     77                 robotMesh = entry.getMesh();
     78             }
     79 
     80             mScript.set_gPFSBackground(ProgramStore.BLEND_NONE_DEPTH_TEST(mRS));
     81 
     82             ProgramFragmentFixedFunction.Builder b = new ProgramFragmentFixedFunction.Builder(mRS);
     83             b.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
     84                          ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
     85             ProgramFragment pfDefault = b.create();
     86             pfDefault.bindSampler(Sampler.CLAMP_LINEAR(mRS), 0);
     87             mScript.set_gPFBackground(pfDefault);
     88 
     89             ProgramVertexFixedFunction.Builder pvb = new ProgramVertexFixedFunction.Builder(mRS);
     90             ProgramVertexFixedFunction pvDefault = pvb.create();
     91             ProgramVertexFixedFunction.Constants va = new ProgramVertexFixedFunction.Constants(mRS);
     92             ((ProgramVertexFixedFunction)pvDefault).bindConstants(va);
     93             mScript.set_gPVBackground(pvDefault);
     94 
     95             long end = System.currentTimeMillis();
     96             Log.v("TIMER", "Loading load time: " + (end - start));
     97             return new Boolean(true);
     98         }
     99 
    100         protected void onPostExecute(Boolean result) {
    101             mScript.set_gRobotTex(robotTex);
    102             mScript.set_gRobotMesh(robotMesh);
    103         }
    104     }
    105 
    106     // Creates a simple script to show a loding screen until everything is initialized
    107     // Could also be used to do some custom renderscript work before handing things over
    108     // to the scenegraph
    109     void renderLoading() {
    110         mScript = new ScriptC_test_app(mRS, mRes, R.raw.test_app);
    111         mRS.bindRootScript(mScript);
    112     }
    113 }
    114