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.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.updateDisplay();
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.setSelection(0, false);
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