15b539461dcc159bd89297443780d635ccc5e3564John Hoford/*
25b539461dcc159bd89297443780d635ccc5e3564John Hoford * Copyright (C) 2015 The Android Open Source Project
35b539461dcc159bd89297443780d635ccc5e3564John Hoford *
45b539461dcc159bd89297443780d635ccc5e3564John Hoford * Licensed under the Apache License, Version 2.0 (the "License");
55b539461dcc159bd89297443780d635ccc5e3564John Hoford * you may not use this file except in compliance with the License.
65b539461dcc159bd89297443780d635ccc5e3564John Hoford * You may obtain a copy of the License at
75b539461dcc159bd89297443780d635ccc5e3564John Hoford *
85b539461dcc159bd89297443780d635ccc5e3564John Hoford *      http://www.apache.org/licenses/LICENSE-2.0
95b539461dcc159bd89297443780d635ccc5e3564John Hoford *
105b539461dcc159bd89297443780d635ccc5e3564John Hoford * Unless required by applicable law or agreed to in writing, software
115b539461dcc159bd89297443780d635ccc5e3564John Hoford * distributed under the License is distributed on an "AS IS" BASIS,
125b539461dcc159bd89297443780d635ccc5e3564John Hoford * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
135b539461dcc159bd89297443780d635ccc5e3564John Hoford * See the License for the specific language governing permissions and
145b539461dcc159bd89297443780d635ccc5e3564John Hoford * limitations under the License.
155b539461dcc159bd89297443780d635ccc5e3564John Hoford */
165b539461dcc159bd89297443780d635ccc5e3564John Hoford
175b539461dcc159bd89297443780d635ccc5e3564John Hofordpackage com.example.android.rs.vr.engine;
185b539461dcc159bd89297443780d635ccc5e3564John Hoford
195b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.graphics.SurfaceTexture;
205b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.Allocation;
215b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.Element;
225b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.RenderScript;
235b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.renderscript.Type;
245b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.util.Log;
255b539461dcc159bd89297443780d635ccc5e3564John Hofordimport android.view.Surface;
265b539461dcc159bd89297443780d635ccc5e3564John Hoford
275b539461dcc159bd89297443780d635ccc5e3564John Hofordpublic class VrState {
285b539461dcc159bd89297443780d635ccc5e3564John Hoford    private static final String LOGTAG = "VrState";
295b539461dcc159bd89297443780d635ccc5e3564John Hoford    public RenderScript mRs;
305b539461dcc159bd89297443780d635ccc5e3564John Hoford    public Volume mVolume;
315b539461dcc159bd89297443780d635ccc5e3564John Hoford    public RsBrickedBitMask mRsMask;
325b539461dcc159bd89297443780d635ccc5e3564John Hoford    public Material mMaterial = new Material();
335b539461dcc159bd89297443780d635ccc5e3564John Hoford    public Cube mCubeVolume;
345b539461dcc159bd89297443780d635ccc5e3564John Hoford    public TriData mCubeScreen;
355b539461dcc159bd89297443780d635ccc5e3564John Hoford    public int mImgWidth;
365b539461dcc159bd89297443780d635ccc5e3564John Hoford    public int mImgHeight;
375b539461dcc159bd89297443780d635ccc5e3564John Hoford    Allocation mzRangeFullAllocation;
385b539461dcc159bd89297443780d635ccc5e3564John Hoford    public Allocation mScrAllocation; // the RGB data out
395b539461dcc159bd89297443780d635ccc5e3564John Hoford    public Transform mTransform = new Transform();
405b539461dcc159bd89297443780d635ccc5e3564John Hoford
415b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void clone(VrState src) {
425b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRs = src.mRs;
435b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVolume = src.mVolume;
445b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRsMask = src.mRsMask;
455b539461dcc159bd89297443780d635ccc5e3564John Hoford        mMaterial = src.mMaterial;
465b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mCubeVolume == null) {
475b539461dcc159bd89297443780d635ccc5e3564John Hoford            mCubeVolume = new Cube();
485b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
495b539461dcc159bd89297443780d635ccc5e3564John Hoford        mCubeVolume.clone(src.mCubeVolume);
505b539461dcc159bd89297443780d635ccc5e3564John Hoford        mCubeScreen = new TriData(src.mCubeScreen);
515b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgWidth = src.mImgWidth;
525b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgHeight = src.mImgHeight;
535b539461dcc159bd89297443780d635ccc5e3564John Hoford        mzRangeFullAllocation = src.mzRangeFullAllocation;
545b539461dcc159bd89297443780d635ccc5e3564John Hoford        mScrAllocation = src.mScrAllocation;
555b539461dcc159bd89297443780d635ccc5e3564John Hoford        mTransform.clone(src.mTransform);
565b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
575b539461dcc159bd89297443780d635ccc5e3564John Hoford
585b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void createOutputAllocation(Surface surface, int w, int h) {
595b539461dcc159bd89297443780d635ccc5e3564John Hoford
605b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mRs == null) {
615b539461dcc159bd89297443780d635ccc5e3564John Hoford            return;
625b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
635b539461dcc159bd89297443780d635ccc5e3564John Hoford
645b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mScrAllocation == null
655b539461dcc159bd89297443780d635ccc5e3564John Hoford                || mScrAllocation.getType().getX() != w
665b539461dcc159bd89297443780d635ccc5e3564John Hoford                || mScrAllocation.getType().getY() != h) {
675b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (mScrAllocation != null) {
685b539461dcc159bd89297443780d635ccc5e3564John Hoford                mScrAllocation.destroy();
695b539461dcc159bd89297443780d635ccc5e3564John Hoford                mScrAllocation = null;
705b539461dcc159bd89297443780d635ccc5e3564John Hoford                Log.v(LOGTAG, " destroy mScrAllocation");
715b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
725b539461dcc159bd89297443780d635ccc5e3564John Hoford            Type.Builder b = new Type.Builder(mRs, Element.RGBA_8888(mRs));
735b539461dcc159bd89297443780d635ccc5e3564John Hoford            b.setX(w);
745b539461dcc159bd89297443780d635ccc5e3564John Hoford            b.setY(h);
755b539461dcc159bd89297443780d635ccc5e3564John Hoford
765b539461dcc159bd89297443780d635ccc5e3564John Hoford            mScrAllocation = Allocation.createTyped(mRs, b.create(),
775b539461dcc159bd89297443780d635ccc5e3564John Hoford                    Allocation.USAGE_IO_OUTPUT | Allocation.USAGE_SCRIPT);
785b539461dcc159bd89297443780d635ccc5e3564John Hoford
795b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
805b539461dcc159bd89297443780d635ccc5e3564John Hoford        mScrAllocation.setSurface(surface);
815b539461dcc159bd89297443780d635ccc5e3564John Hoford
825b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mzRangeFullAllocation == null
835b539461dcc159bd89297443780d635ccc5e3564John Hoford                || mzRangeFullAllocation.getType().getX() != w
845b539461dcc159bd89297443780d635ccc5e3564John Hoford                || mzRangeFullAllocation.getType().getY() != h) {
855b539461dcc159bd89297443780d635ccc5e3564John Hoford            if (mzRangeFullAllocation != null) {
865b539461dcc159bd89297443780d635ccc5e3564John Hoford                mzRangeFullAllocation.destroy();
875b539461dcc159bd89297443780d635ccc5e3564John Hoford                mzRangeFullAllocation = null;
885b539461dcc159bd89297443780d635ccc5e3564John Hoford                Log.v(LOGTAG, " destroy mzRangeFullAllocation");
895b539461dcc159bd89297443780d635ccc5e3564John Hoford
905b539461dcc159bd89297443780d635ccc5e3564John Hoford            }
915b539461dcc159bd89297443780d635ccc5e3564John Hoford            Type.Builder b = new Type.Builder(mRs, Element.F32_2(mRs));
925b539461dcc159bd89297443780d635ccc5e3564John Hoford            b.setX(w);
935b539461dcc159bd89297443780d635ccc5e3564John Hoford            b.setY(h);
945b539461dcc159bd89297443780d635ccc5e3564John Hoford
955b539461dcc159bd89297443780d635ccc5e3564John Hoford            mzRangeFullAllocation = Allocation.createTyped(mRs, b.create());
965b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
975b539461dcc159bd89297443780d635ccc5e3564John Hoford
985b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgWidth = w;
995b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgHeight = h;
1005b539461dcc159bd89297443780d635ccc5e3564John Hoford        mTransform.setScreenDim(w, h);
1015b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1025b539461dcc159bd89297443780d635ccc5e3564John Hoford
1035b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void copyData(VrState src) {
1045b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRs = src.mRs;
1055b539461dcc159bd89297443780d635ccc5e3564John Hoford        mVolume = src.mVolume;
1065b539461dcc159bd89297443780d635ccc5e3564John Hoford        mRsMask = src.mRsMask;
1075b539461dcc159bd89297443780d635ccc5e3564John Hoford        mMaterial = src.mMaterial;
1085b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mCubeVolume == null) {
1095b539461dcc159bd89297443780d635ccc5e3564John Hoford            mCubeVolume = new Cube();
1105b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1115b539461dcc159bd89297443780d635ccc5e3564John Hoford        mCubeVolume.clone(src.mCubeVolume);
1125b539461dcc159bd89297443780d635ccc5e3564John Hoford        mCubeScreen = new TriData(src.mCubeScreen); // TODO I should not have to do new each time
1135b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgWidth = src.mImgWidth;
1145b539461dcc159bd89297443780d635ccc5e3564John Hoford        mImgHeight = src.mImgHeight;
1155b539461dcc159bd89297443780d635ccc5e3564John Hoford        mTransform.clone(src.mTransform);
1165b539461dcc159bd89297443780d635ccc5e3564John Hoford
1175b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1185b539461dcc159bd89297443780d635ccc5e3564John Hoford
1195b539461dcc159bd89297443780d635ccc5e3564John Hoford    public void destroyScreenAllocation() {
1205b539461dcc159bd89297443780d635ccc5e3564John Hoford        Log.v(LOGTAG, "destroyScreenAllocation");
1215b539461dcc159bd89297443780d635ccc5e3564John Hoford        if (mScrAllocation != null) {
1225b539461dcc159bd89297443780d635ccc5e3564John Hoford            mScrAllocation.destroy();
1235b539461dcc159bd89297443780d635ccc5e3564John Hoford            mScrAllocation = null;
1245b539461dcc159bd89297443780d635ccc5e3564John Hoford        }
1255b539461dcc159bd89297443780d635ccc5e3564John Hoford    }
1265b539461dcc159bd89297443780d635ccc5e3564John Hoford}
127