1 /*
2  * Copyright (C) 2015 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.example.android.rs.vr.engine;
18 
19 import android.graphics.SurfaceTexture;
20 import android.renderscript.Allocation;
21 import android.renderscript.Element;
22 import android.renderscript.RenderScript;
23 import android.renderscript.Type;
24 import android.util.Log;
25 import android.view.Surface;
26 
27 public class VrState {
28     private static final String LOGTAG = "VrState";
29     public RenderScript mRs;
30     public Volume mVolume;
31     public RsBrickedBitMask mRsMask;
32     public Material mMaterial = new Material();
33     public Cube mCubeVolume;
34     public TriData mCubeScreen;
35     public int mImgWidth;
36     public int mImgHeight;
37     Allocation mzRangeFullAllocation;
38     public Allocation mScrAllocation; // the RGB data out
39     public Transform mTransform = new Transform();
40 
clone(VrState src)41     public void clone(VrState src) {
42         mRs = src.mRs;
43         mVolume = src.mVolume;
44         mRsMask = src.mRsMask;
45         mMaterial = src.mMaterial;
46         if (mCubeVolume == null) {
47             mCubeVolume = new Cube();
48         }
49         mCubeVolume.clone(src.mCubeVolume);
50         mCubeScreen = new TriData(src.mCubeScreen);
51         mImgWidth = src.mImgWidth;
52         mImgHeight = src.mImgHeight;
53         mzRangeFullAllocation = src.mzRangeFullAllocation;
54         mScrAllocation = src.mScrAllocation;
55         mTransform.clone(src.mTransform);
56     }
57 
createOutputAllocation(Surface surface, int w, int h)58     public void createOutputAllocation(Surface surface, int w, int h) {
59 
60         if (mRs == null) {
61             return;
62         }
63 
64         if (mScrAllocation == null
65                 || mScrAllocation.getType().getX() != w
66                 || mScrAllocation.getType().getY() != h) {
67             if (mScrAllocation != null) {
68                 mScrAllocation.destroy();
69                 mScrAllocation = null;
70                 Log.v(LOGTAG, " destroy mScrAllocation");
71             }
72             Type.Builder b = new Type.Builder(mRs, Element.RGBA_8888(mRs));
73             b.setX(w);
74             b.setY(h);
75 
76             mScrAllocation = Allocation.createTyped(mRs, b.create(),
77                     Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
78 
79         }
80         mScrAllocation.setSurface(surface);
81 
82         if (mzRangeFullAllocation == null
83                 || mzRangeFullAllocation.getType().getX() != w
84                 || mzRangeFullAllocation.getType().getY() != h) {
85             if (mzRangeFullAllocation != null) {
86                 mzRangeFullAllocation.destroy();
87                 mzRangeFullAllocation = null;
88                 Log.v(LOGTAG, " destroy mzRangeFullAllocation");
89 
90             }
91             Type.Builder b = new Type.Builder(mRs, Element.F32_2(mRs));
92             b.setX(w);
93             b.setY(h);
94 
95             mzRangeFullAllocation = Allocation.createTyped(mRs, b.create());
96         }
97 
98         mImgWidth = w;
99         mImgHeight = h;
100         mTransform.setScreenDim(w, h);
101     }
102 
copyData(VrState src)103     public void copyData(VrState src) {
104         mRs = src.mRs;
105         mVolume = src.mVolume;
106         mRsMask = src.mRsMask;
107         mMaterial = src.mMaterial;
108         if (mCubeVolume == null) {
109             mCubeVolume = new Cube();
110         }
111         mCubeVolume.clone(src.mCubeVolume);
112         mCubeScreen = new TriData(src.mCubeScreen); // TODO I should not have to do new each time
113         mImgWidth = src.mImgWidth;
114         mImgHeight = src.mImgHeight;
115         mTransform.clone(src.mTransform);
116 
117     }
118 
destroyScreenAllocation()119     public void destroyScreenAllocation() {
120         Log.v(LOGTAG, "destroyScreenAllocation");
121         if (mScrAllocation != null) {
122             mScrAllocation.destroy();
123             mScrAllocation = null;
124         }
125     }
126 }
127