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.rs.image2; 18 19import java.lang.Math; 20 21import android.support.v8.renderscript.*; 22import android.util.Log; 23 24public class GroupTest extends TestBase { 25 private ScriptIntrinsicConvolve3x3 mConvolve; 26 private ScriptIntrinsicColorMatrix mMatrix; 27 28 private Allocation mScratchPixelsAllocation1; 29 private ScriptGroup mGroup; 30 31 private int mWidth; 32 private int mHeight; 33 private boolean mUseNative; 34 35 36 public GroupTest(boolean useNative) { 37 mUseNative = useNative; 38 } 39 40 public void createTest(android.content.res.Resources res) { 41 mWidth = mInPixelsAllocation.getType().getX(); 42 mHeight = mInPixelsAllocation.getType().getY(); 43 44 mConvolve = ScriptIntrinsicConvolve3x3.create(mRS, Element.U8_4(mRS)); 45 mMatrix = ScriptIntrinsicColorMatrix.create(mRS, Element.U8_4(mRS)); 46 47 float f[] = new float[9]; 48 f[0] = 0.f; f[1] = -1.f; f[2] = 0.f; 49 f[3] = -1.f; f[4] = 5.f; f[5] = -1.f; 50 f[6] = 0.f; f[7] = -1.f; f[8] = 0.f; 51 mConvolve.setCoefficients(f); 52 53 Matrix4f m = new Matrix4f(); 54 m.set(1, 0, 0.2f); 55 m.set(1, 1, 0.9f); 56 m.set(1, 2, 0.2f); 57 mMatrix.setColorMatrix(m); 58 59 Type.Builder tb = new Type.Builder(mRS, Element.U8_4(mRS)); 60 tb.setX(mWidth); 61 tb.setY(mHeight); 62 Type connect = tb.create(); 63 64 if (mUseNative) { 65 ScriptGroup.Builder b = new ScriptGroup.Builder(mRS); 66 b.addKernel(mConvolve.getKernelID()); 67 b.addKernel(mMatrix.getKernelID()); 68 b.addConnection(connect, mConvolve.getKernelID(), mMatrix.getKernelID()); 69 mGroup = b.create(); 70 } else { 71 mScratchPixelsAllocation1 = Allocation.createTyped(mRS, connect); 72 } 73 } 74 75 public void animateBars(float time) { 76 Matrix4f m = new Matrix4f(); 77 m.set(1, 0, (time + 0.2f) % 1.0f); 78 m.set(1, 1, (time + 0.9f) % 1.0f); 79 m.set(1, 2, (time + 0.4f) % 1.0f); 80 mMatrix.setColorMatrix(m); 81 } 82 83 public void runTest() { 84 mConvolve.setInput(mInPixelsAllocation); 85 if (mUseNative) { 86 mGroup.setOutput(mMatrix.getKernelID(), mOutPixelsAllocation); 87 mGroup.execute(); 88 } else { 89 mConvolve.forEach(mScratchPixelsAllocation1); 90 mMatrix.forEach(mScratchPixelsAllocation1, mOutPixelsAllocation); 91 } 92 } 93 94} 95