summaryrefslogtreecommitdiffstats
path: root/libs/rs/java/Film/src/com/android/film/FilmRS.java
blob: dccc1d210ea28af82debf855056f4549efd8d62a (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
/*
 * Copyright (C) 2008 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.film;

import java.io.Writer;

import android.content.Context;
import android.content.res.Resources;
import android.graphics.Bitmap;
import android.util.Log;

import android.renderscript.*;

public class FilmRS {
    class StripPosition {
        public float translate;
        public float rotate;
        public float focus;
        public int triangleOffsetCount;
    }
    StripPosition mPos = new StripPosition();


    private final int STATE_LAST_FOCUS = 1;

    public FilmRS() {
    }

    public void init(RenderScript rs, Resources res, int width, int height) {
        mRS = rs;
        mRes = res;
        initRS();
    }

    public void setFilmStripPosition(int x, int y)
    {
        if (x < 50) {
            x = 50;
        }
        if (x > 270) {
            x = 270;
        }

        float anim = ((float)x-50) / 270.f;
        mPos.translate = 2f * anim + 0.5f;   // translation
        mPos.rotate = (anim * 40);  // rotation
        mPos.focus = ((float)y) / 16.f - 10.f;  // focusPos
        mPos.triangleOffsetCount = mFSM.mTriangleOffsetsCount;
        mAllocPos.data(mPos);
    }


    private Resources mRes;
    private RenderScript mRS;
    private Script mScriptStrip;
    private Script mScriptImage;
    private Sampler mSampler;
    private ProgramStore mPSBackground;
    private ProgramStore mPSImages;
    private ProgramFragment mPFBackground;
    private ProgramFragment mPFImages;
    private ProgramVertex mPVBackground;
    private ProgramVertex mPVImages;
    private ProgramVertex.MatrixAllocation mPVA;
    private Type mStripPositionType;

    private Allocation mImages[];
    private Allocation mAllocIDs;
    private Allocation mAllocPos;
    private Allocation mAllocState;
    private Allocation mAllocPV;
    private Allocation mAllocOffsetsTex;
    private Allocation mAllocOffsets;

    private SimpleMesh mMesh;
    private Light mLight;

    private FilmStripMesh mFSM;

    private int[] mBufferIDs;
    private float[] mBufferPos = new float[3];
    private int[] mBufferState;

    private void initPFS() {
        ProgramStore.Builder b = new ProgramStore.Builder(mRS, null, null);

        b.setDepthFunc(ProgramStore.DepthFunc.LESS);
        b.setDitherEnable(true);
        b.setDepthMask(true);
        mPSBackground = b.create();
        mPSBackground.setName("PSBackground");

        b.setDepthFunc(ProgramStore.DepthFunc.EQUAL);
        b.setDitherEnable(false);
        b.setDepthMask(false);
        b.setBlendFunc(ProgramStore.BlendSrcFunc.ONE,
                       ProgramStore.BlendDstFunc.ONE);
        mPSImages = b.create();
        mPSImages.setName("PSImages");
    }

    private void initPF() {
        Sampler.Builder bs = new Sampler.Builder(mRS);
        bs.setMin(Sampler.Value.LINEAR);//_MIP_LINEAR);
        bs.setMag(Sampler.Value.LINEAR);
        bs.setWrapS(Sampler.Value.CLAMP);
        bs.setWrapT(Sampler.Value.WRAP);
        mSampler = bs.create();

        ProgramFragment.Builder b = new ProgramFragment.Builder(mRS);
        mPFBackground = b.create();
        mPFBackground.setName("PFBackground");

        b = new ProgramFragment.Builder(mRS);
        b.setTexture(ProgramFragment.Builder.EnvMode.REPLACE,
                     ProgramFragment.Builder.Format.RGBA, 0);
        mPFImages = b.create();
        mPFImages.bindSampler(mSampler, 0);
        mPFImages.setName("PFImages");
    }

    private void initPV() {
        mLight = (new Light.Builder(mRS)).create();
        mLight.setPosition(0, -0.5f, -1.0f);

        ProgramVertex.Builder pvb = new ProgramVertex.Builder(mRS, null, null);
        //pvb.addLight(mLight);
        mPVBackground = pvb.create();
        mPVBackground.setName("PVBackground");

        pvb = new ProgramVertex.Builder(mRS, null, null);
        pvb.setTextureMatrixEnable(true);
        mPVImages = pvb.create();
        mPVImages.setName("PVImages");
    }

    private void loadImages() {
        mBufferIDs = new int[13];
        mImages = new Allocation[13];
        mAllocIDs = Allocation.createSized(mRS,
            Element.USER_F32(mRS), mBufferIDs.length);

        Element ie = Element.RGB_565(mRS);
        mImages[0] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p01, ie, true);
        mImages[1] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p02, ie, true);
        mImages[2] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p03, ie, true);
        mImages[3] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p04, ie, true);
        mImages[4] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p05, ie, true);
        mImages[5] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p06, ie, true);
        mImages[6] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p07, ie, true);
        mImages[7] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p08, ie, true);
        mImages[8] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p09, ie, true);
        mImages[9] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p10, ie, true);
        mImages[10] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p11, ie, true);
        mImages[11] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p12, ie, true);
        mImages[12] = Allocation.createFromBitmapResourceBoxed(mRS, mRes, R.drawable.p13, ie, true);

        int black[] = new int[1024];
        for(int ct=0; ct < mImages.length; ct++) {
            Allocation.Adapter2D a = mImages[ct].createAdapter2D();

            int size = 512;
            int mip = 0;
            while(size >= 2) {
                a.subData(0, 0, 2, size, black);
                a.subData(size-2, 0, 2, size, black);
                a.subData(0, 0, size, 2, black);
                a.subData(0, size-2, size, 2, black);
                size >>= 1;
                mip++;
                a.setConstraint(Dimension.LOD, mip);
            }

            mImages[ct].uploadToTexture(1);
            mBufferIDs[ct] = mImages[ct].getID();
        }
        mAllocIDs.data(mBufferIDs);
    }

    private void initState()
    {
        mBufferState = new int[10];
        mAllocState = Allocation.createSized(mRS,
            Element.USER_F32(mRS), mBufferState.length);
        mBufferState[STATE_LAST_FOCUS] = -1;
        mAllocState.data(mBufferState);
    }

    private void initRS() {
        mFSM = new FilmStripMesh();
        mMesh = mFSM.init(mRS);
        mMesh.setName("mesh");

        initPFS();
        initPF();
        initPV();

        Log.e("rs", "Done loading named");

        mStripPositionType = Type.createFromClass(mRS, StripPosition.class, 1);

        ScriptC.Builder sb = new ScriptC.Builder(mRS);
        sb.setScript(mRes, R.raw.filmstrip);
        sb.setRoot(true);
        sb.setType(mStripPositionType, "Pos", 1);
        mScriptStrip = sb.create();
        mScriptStrip.setClearColor(0.0f, 0.0f, 0.0f, 1.0f);

        mAllocPos = Allocation.createTyped(mRS, mStripPositionType);

        loadImages();
        initState();

        mPVA = new ProgramVertex.MatrixAllocation(mRS);
        mPVBackground.bindAllocation(mPVA);
        mPVImages.bindAllocation(mPVA);
        mPVA.setupProjectionNormalized(320, 480);


        mScriptStrip.bindAllocation(mAllocIDs, 0);
        mScriptStrip.bindAllocation(mAllocPos, 1);
        mScriptStrip.bindAllocation(mAllocState, 2);
        mScriptStrip.bindAllocation(mPVA.mAlloc, 3);


        mAllocOffsets = Allocation.createSized(mRS,
            Element.USER_I32(mRS), mFSM.mTriangleOffsets.length);
        mAllocOffsets.data(mFSM.mTriangleOffsets);
        mScriptStrip.bindAllocation(mAllocOffsets, 4);

        mAllocOffsetsTex = Allocation.createSized(mRS,
            Element.USER_F32(mRS), mFSM.mTriangleOffsetsTex.length);
        mAllocOffsetsTex.data(mFSM.mTriangleOffsetsTex);
        mScriptStrip.bindAllocation(mAllocOffsetsTex, 5);

        setFilmStripPosition(0, 0);
        mRS.contextBindRootScript(mScriptStrip);
    }
}