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