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.imagejb;
18
19import java.lang.Math;
20import java.lang.Short;
21
22import android.renderscript.Allocation;
23import android.renderscript.Element;
24import android.renderscript.Matrix4f;
25import android.renderscript.RenderScript;
26import android.renderscript.Script;
27import android.renderscript.ScriptC;
28import android.renderscript.ScriptIntrinsicBlend;
29import android.renderscript.Type;
30import android.util.Log;
31import android.widget.SeekBar;
32import android.widget.TextView;
33import android.widget.AdapterView;
34import android.widget.ArrayAdapter;
35import android.view.View;
36import android.widget.Spinner;
37
38public class Blend extends TestBase {
39    private ScriptIntrinsicBlend mBlend;
40    private ScriptC_blend mBlendHelper;
41    private short image1Alpha = 128;
42    private short image2Alpha = 128;
43
44    String mIntrinsicNames[];
45
46    private Allocation image1;
47    private Allocation image2;
48    private int currentIntrinsic = 0;
49
50    private AdapterView.OnItemSelectedListener mIntrinsicSpinnerListener =
51            new AdapterView.OnItemSelectedListener() {
52                public void onItemSelected(AdapterView<?> parent, View view, int pos, long id) {
53                    currentIntrinsic = pos;
54                    if (mRS != null) {
55                        runTest();
56                        act.mProcessor.update();
57                    }
58                }
59
60                public void onNothingSelected(AdapterView parent) {
61
62                }
63            };
64
65    public void createTest(android.content.res.Resources res) {
66        mBlend = ScriptIntrinsicBlend.create(mRS, Element.U8_4(mRS));
67        mBlendHelper = new ScriptC_blend(mRS);
68        mBlendHelper.set_alpha((short)128);
69
70        image1 = Allocation.createTyped(mRS, mInPixelsAllocation.getType());
71        image2 = Allocation.createTyped(mRS, mInPixelsAllocation2.getType());
72
73        mIntrinsicNames = new String[14];
74        mIntrinsicNames[0] = "Source";
75        mIntrinsicNames[1] = "Destination";
76        mIntrinsicNames[2] = "Source Over";
77        mIntrinsicNames[3] = "Destination Over";
78        mIntrinsicNames[4] = "Source In";
79        mIntrinsicNames[5] = "Destination In";
80        mIntrinsicNames[6] = "Source Out";
81        mIntrinsicNames[7] = "Destination Out";
82        mIntrinsicNames[8] = "Source Atop";
83        mIntrinsicNames[9] = "Destination Atop";
84        mIntrinsicNames[10] = "XOR";
85        mIntrinsicNames[11] = "Add";
86        mIntrinsicNames[12] = "Subtract";
87        mIntrinsicNames[13] = "Multiply";
88    }
89
90    public boolean onSpinner1Setup(Spinner s) {
91        s.setAdapter(new ArrayAdapter<String>(
92            act, R.layout.spinner_layout, mIntrinsicNames));
93        s.setOnItemSelectedListener(mIntrinsicSpinnerListener);
94        return true;
95    }
96
97    public boolean onBar1Setup(SeekBar b, TextView t) {
98        t.setText("Image 1 Alpha");
99        b.setMax(255);
100        b.setProgress(image1Alpha);
101        return true;
102    }
103
104    public void onBar1Changed(int progress) {
105        image1Alpha = (short)progress;
106    }
107
108    public boolean onBar2Setup(SeekBar b, TextView t) {
109        t.setText("Image 2 Alpha");
110        b.setMax(255);
111        b.setProgress(image2Alpha);
112        return true;
113    }
114
115    public void onBar2Changed(int progress) {
116        image2Alpha = (short)progress;
117    }
118
119    public void runTest() {
120        image1.copy2DRangeFrom(0, 0, mInPixelsAllocation.getType().getX(), mInPixelsAllocation.getType().getY(), mInPixelsAllocation, 0, 0);
121        image2.copy2DRangeFrom(0, 0, mInPixelsAllocation2.getType().getX(), mInPixelsAllocation2.getType().getY(), mInPixelsAllocation2, 0, 0);
122
123        mBlendHelper.set_alpha(image1Alpha);
124        mBlendHelper.forEach_setImageAlpha(image1, image1);
125
126        mBlendHelper.set_alpha(image2Alpha);
127        mBlendHelper.forEach_setImageAlpha(image2, image2);
128
129        switch (currentIntrinsic) {
130        case 0:
131            mBlend.forEachSrc(image1, image2);
132            break;
133        case 1:
134            mBlend.forEachDst(image1, image2);
135            break;
136        case 2:
137            mBlend.forEachSrcOver(image1, image2);
138            break;
139        case 3:
140            mBlend.forEachDstOver(image1, image2);
141            break;
142        case 4:
143            mBlend.forEachSrcIn(image1, image2);
144            break;
145        case 5:
146            mBlend.forEachDstIn(image1, image2);
147            break;
148        case 6:
149            mBlend.forEachSrcOut(image1, image2);
150            break;
151        case 7:
152            mBlend.forEachDstOut(image1, image2);
153            break;
154        case 8:
155            mBlend.forEachSrcAtop(image1, image2);
156            break;
157        case 9:
158            mBlend.forEachDstAtop(image1, image2);
159            break;
160        case 10:
161            mBlend.forEachXor(image1, image2);
162            break;
163        case 11:
164            mBlend.forEachAdd(image1, image2);
165            break;
166        case 12:
167            mBlend.forEachSubtract(image1, image2);
168            break;
169        case 13:
170            mBlend.forEachMultiply(image1, image2);
171            break;
172        }
173
174        mOutPixelsAllocation.copy2DRangeFrom(0, 0, image2.getType().getX(), image2.getType().getY(), image2, 0, 0);
175    }
176
177}
178