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.example.android.rs.sto; 18 19import android.content.res.Resources; 20import android.renderscript.*; 21import android.graphics.SurfaceTexture; 22import android.util.Log; 23 24 25public class SurfaceTextureOpaqueRS { 26 static final private int NUM_CAMERA_PREVIEW_BUFFERS = 2; 27 28 public SurfaceTextureOpaqueRS() { 29 } 30 31 private Resources mRes; 32 private RenderScriptGL mRS; 33 private ScriptC_sto mScript; 34 private SurfaceTexture mST; 35 private Allocation mSto; 36 private Allocation mSto2; 37 private Allocation mRto; 38 private ProgramFragment mPF; 39 40 public void init(RenderScriptGL rs, Resources res) { 41 mRS = rs; 42 mRes = res; 43 44 Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS)); 45 tb.setX(640); 46 tb.setY(480); 47 mSto = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_TEXTURE | 48 Allocation.USAGE_IO_INPUT); 49 mRto = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_RENDER_TARGET | 50 Allocation.USAGE_IO_OUTPUT); 51 mSto2 = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_TEXTURE | 52 Allocation.USAGE_IO_INPUT); 53 mST = mSto.getSurfaceTexture(); 54 mRto.setSurfaceTexture(mSto2.getSurfaceTexture()); 55 56 ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs); 57 pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE, 58 ProgramFragmentFixedFunction.Builder.Format.RGBA, 0); 59 mPF = pfb.create(); 60 mPF.bindSampler(Sampler.CLAMP_NEAREST(mRS), 0); 61 rs.bindProgramFragment(mPF); 62 63 mScript = new ScriptC_sto(mRS, mRes, R.raw.sto); 64 mScript.set_sto(mSto); 65 mScript.set_rto(mRto); 66 mScript.set_sto2(mSto2); 67 mScript.set_pf(mPF); 68 69 mRS.bindRootScript(mScript); 70 71 72 android.util.Log.v("sto", "Init complete"); 73 } 74 75 SurfaceTexture getST() { 76 return mST; 77 } 78 79 public void newFrame() { 80 mSto.ioReceive(); 81 } 82 83} 84