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