Skip to content

Commit 74478f7

Browse files
author
Tim Murray
committed
fix blend intrinsics, add tests
Bug: 7190126 Change-Id: If69213377282bf5b412508e7af974a1f8d440287
1 parent fea9df6 commit 74478f7

File tree

5 files changed

+220
-12
lines changed

5 files changed

+220
-12
lines changed

graphics/java/android/renderscript/ScriptIntrinsicBlend.java

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -36,17 +36,18 @@ public class ScriptIntrinsicBlend extends ScriptIntrinsic {
3636
* @return ScriptIntrinsicBlend
3737
*/
3838
public static ScriptIntrinsicBlend create(RenderScript rs, Element e) {
39-
int id = rs.nScriptIntrinsicCreate(6, e.getID(rs));
39+
// 7 comes from RS_SCRIPT_INTRINSIC_ID_BLEND in rsDefines.h
40+
int id = rs.nScriptIntrinsicCreate(7, e.getID(rs));
4041
return new ScriptIntrinsicBlend(id, rs);
4142

4243
}
4344

4445
private void blend(int id, Allocation ain, Allocation aout) {
45-
if (ain.getElement() != Element.U8_4(mRS)) {
46-
throw new RSIllegalArgumentException("Input not of expected format.");
46+
if (!ain.getElement().isCompatible(Element.U8_4(mRS))) {
47+
throw new RSIllegalArgumentException("Input is not of expected format.");
4748
}
48-
if (aout.getElement() != Element.U8_4(mRS)) {
49-
throw new RSIllegalArgumentException("Output not of expected format.");
49+
if (!aout.getElement().isCompatible(Element.U8_4(mRS))) {
50+
throw new RSIllegalArgumentException("Output is not of expected format.");
5051
}
5152
forEach(id, ain, aout, null);
5253
}

tests/RenderScriptTests/ImageProcessing/res/layout/main.xml

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

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/ImageProcessingActivity.java

Lines changed: 20 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -55,9 +55,11 @@ public class ImageProcessingActivity extends Activity
5555
private final String RESULT_FILE = "image_processing_result.csv";
5656

5757
Bitmap mBitmapIn;
58+
Bitmap mBitmapIn2;
5859
Bitmap mBitmapOut;
5960
String mTestNames[];
6061

62+
private Spinner mSpinner;
6163
private SeekBar mBar1;
6264
private SeekBar mBar2;
6365
private SeekBar mBar3;
@@ -81,6 +83,10 @@ public class ImageProcessingActivity extends Activity
8183

8284
private TestBase mTest;
8385

86+
public void updateDisplay() {
87+
mTest.updateBitmap(mBitmapOut);
88+
mDisplayView.invalidate();
89+
}
8490

8591
public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
8692
if (fromUser) {
@@ -98,8 +104,7 @@ public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
98104
}
99105

100106
mTest.runTest();
101-
mTest.updateBitmap(mBitmapOut);
102-
mDisplayView.invalidate();
107+
updateDisplay();
103108
}
104109
}
105110

@@ -110,6 +115,9 @@ public void onStopTrackingTouch(SeekBar seekBar) {
110115
}
111116

112117
void setupBars() {
118+
mSpinner.setVisibility(View.VISIBLE);
119+
mTest.onSpinner1Setup(mSpinner);
120+
113121
mBar1.setVisibility(View.VISIBLE);
114122
mText1.setVisibility(View.VISIBLE);
115123
mTest.onBar1Setup(mBar1, mText1);
@@ -221,19 +229,21 @@ void changeTest(int testID) {
221229
case 27:
222230
mTest = new Mandelbrot();
223231
break;
232+
case 28:
233+
mTest = new Blend();
234+
break;
224235
}
225236

226-
mTest.createBaseTest(this, mBitmapIn);
237+
mTest.createBaseTest(this, mBitmapIn, mBitmapIn2);
227238
setupBars();
228239

229240
mTest.runTest();
230-
mTest.updateBitmap(mBitmapOut);
231-
mDisplayView.invalidate();
241+
updateDisplay();
232242
mBenchmarkResult.setText("Result: not run");
233243
}
234244

235245
void setupTests() {
236-
mTestNames = new String[28];
246+
mTestNames = new String[29];
237247
mTestNames[0] = "Levels Vec3 Relaxed";
238248
mTestNames[1] = "Levels Vec4 Relaxed";
239249
mTestNames[2] = "Levels Vec3 Full";
@@ -262,6 +272,7 @@ void setupTests() {
262272
mTestNames[25] = "Convolve 5x5";
263273
mTestNames[26] = "Intrinsics Convolve 5x5";
264274
mTestNames[27] = "Mandelbrot";
275+
mTestNames[28] = "Intrinsics Blend";
265276

266277
mTestSpinner.setAdapter(new ArrayAdapter<String>(
267278
this, R.layout.spinner_layout, mTestNames));
@@ -284,13 +295,16 @@ protected void onCreate(Bundle savedInstanceState) {
284295
setContentView(R.layout.main);
285296

286297
mBitmapIn = loadBitmap(R.drawable.img1600x1067);
298+
mBitmapIn2 = loadBitmap(R.drawable.img1600x1067b);
287299
mBitmapOut = loadBitmap(R.drawable.img1600x1067);
288300

289301
mSurfaceView = (SurfaceView) findViewById(R.id.surface);
290302

291303
mDisplayView = (ImageView) findViewById(R.id.display);
292304
mDisplayView.setImageBitmap(mBitmapOut);
293305

306+
mSpinner = (Spinner) findViewById(R.id.spinner1);
307+
294308
mBar1 = (SeekBar) findViewById(R.id.slider1);
295309
mBar2 = (SeekBar) findViewById(R.id.slider2);
296310
mBar3 = (SeekBar) findViewById(R.id.slider3);

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/TestBase.java

Lines changed: 14 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,14 +36,18 @@
3636
import android.view.View;
3737
import android.util.Log;
3838
import java.lang.Math;
39+
import android.widget.Spinner;
3940

4041
public class TestBase {
4142
protected final String TAG = "Img";
4243

4344
protected RenderScript mRS;
4445
protected Allocation mInPixelsAllocation;
46+
protected Allocation mInPixelsAllocation2;
4547
protected Allocation mOutPixelsAllocation;
4648

49+
protected ImageProcessingActivity act;
50+
4751
// Override to use UI elements
4852
public void onBar1Changed(int progress) {
4953
}
@@ -84,11 +88,20 @@ public boolean onBar5Setup(SeekBar b, TextView t) {
8488
return false;
8589
}
8690

87-
public final void createBaseTest(ImageProcessingActivity act, Bitmap b) {
91+
public boolean onSpinner1Setup(Spinner s) {
92+
s.setVisibility(View.INVISIBLE);
93+
return false;
94+
}
95+
96+
public final void createBaseTest(ImageProcessingActivity ipact, Bitmap b, Bitmap b2) {
97+
act = ipact;
8898
mRS = RenderScript.create(act);
8999
mInPixelsAllocation = Allocation.createFromBitmap(mRS, b,
90100
Allocation.MipmapControl.MIPMAP_NONE,
91101
Allocation.USAGE_SCRIPT);
102+
mInPixelsAllocation2 = Allocation.createFromBitmap(mRS, b2,
103+
Allocation.MipmapControl.MIPMAP_NONE,
104+
Allocation.USAGE_SCRIPT);
92105
mOutPixelsAllocation = Allocation.createFromBitmap(mRS, b,
93106
Allocation.MipmapControl.MIPMAP_NONE,
94107
Allocation.USAGE_SCRIPT);

0 commit comments

Comments
 (0)