Skip to content

Commit 42f23b3

Browse files
Alex SakhartchoukAndroid (Google) Code Review
authored andcommitted
Merge "Adding more modes to the sampler test app. Fixing pixel placement."
2 parents 761415b + e51ae26 commit 42f23b3

File tree

4 files changed

+160
-74
lines changed

4 files changed

+160
-74
lines changed

tests/RenderScriptTests/SampleTest/res/layout/rs.xml

Lines changed: 32 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,10 +26,42 @@
2626
android:orientation="vertical"
2727
android:layout_width="fill_parent"
2828
android:layout_height="fill_parent">
29+
<TextView
30+
android:layout_width="match_parent"
31+
android:layout_height="wrap_content"
32+
android:textSize="8pt"
33+
android:text="@string/wraplinear"/>
2934
<TextureView
3035
android:id="@+id/display"
3136
android:layout_width="256sp"
3237
android:layout_height="256sp" />
38+
<TextView
39+
android:layout_width="match_parent"
40+
android:layout_height="wrap_content"
41+
android:textSize="8pt"
42+
android:text="@string/clamplinear"/>
43+
<TextureView
44+
android:id="@+id/display2"
45+
android:layout_width="256sp"
46+
android:layout_height="256sp" />
47+
<TextView
48+
android:layout_width="match_parent"
49+
android:layout_height="wrap_content"
50+
android:textSize="8pt"
51+
android:text="@string/wrapnearest"/>
52+
<TextureView
53+
android:id="@+id/display3"
54+
android:layout_width="256sp"
55+
android:layout_height="256sp" />
56+
<TextView
57+
android:layout_width="match_parent"
58+
android:layout_height="wrap_content"
59+
android:textSize="8pt"
60+
android:text="@string/clampnearest"/>
61+
<TextureView
62+
android:id="@+id/display4"
63+
android:layout_width="256sp"
64+
android:layout_height="256sp" />
3365
<LinearLayout xmlns:android="http://schemas.android.com/apk/res/android"
3466
android:orientation="horizontal"
3567
android:layout_width="fill_parent"

tests/RenderScriptTests/SampleTest/res/values/strings.xml

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,4 +21,8 @@
2121
<!-- General -->
2222
<skip />
2323
<string name="benchmark">Benchmark</string>
24+
<string name="wraplinear">Wrap Linear</string>
25+
<string name="clamplinear">Clamp Linear</string>
26+
<string name="wrapnearest">Wrap Nearest</string>
27+
<string name="clampnearest">Clamp Nearest</string>
2428
</resources>

tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/SampleRSActivity.java

Lines changed: 64 additions & 41 deletions
Original file line numberDiff line numberDiff line change
@@ -31,14 +31,40 @@
3131
import android.renderscript.Type.Builder;
3232
import android.util.Log;
3333
import android.view.TextureView;
34+
import android.view.TextureView.SurfaceTextureListener;
3435
import android.view.View;
3536
import android.widget.ImageView;
3637
import android.widget.SeekBar;
3738
import android.widget.TextView;
3839

39-
public class SampleRSActivity extends Activity
40-
implements TextureView.SurfaceTextureListener
41-
{
40+
public class SampleRSActivity extends Activity {
41+
class TextureViewUpdater implements TextureView.SurfaceTextureListener {
42+
private Allocation mOutPixelsAllocation;
43+
private Sampler mSampler;
44+
45+
TextureViewUpdater(Allocation outAlloc, Sampler sampler) {
46+
mOutPixelsAllocation = outAlloc;
47+
mSampler = sampler;
48+
}
49+
50+
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
51+
}
52+
53+
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
54+
mOutPixelsAllocation.setSurfaceTexture(surface);
55+
}
56+
57+
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
58+
mOutPixelsAllocation.setSurfaceTexture(surface);
59+
filterAlloc(mOutPixelsAllocation, mSampler);
60+
}
61+
62+
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
63+
mOutPixelsAllocation.setSurfaceTexture(null);
64+
return true;
65+
}
66+
}
67+
4268
private final String TAG = "Img";
4369
private Bitmap mBitmapIn;
4470
private TextureView mDisplayView;
@@ -47,7 +73,6 @@ public class SampleRSActivity extends Activity
4773

4874
private RenderScript mRS;
4975
private Allocation mInPixelsAllocation;
50-
private Allocation mOutPixelsAllocation;
5176
private ScriptC_sample mScript;
5277

5378
public void onStartTrackingTouch(SeekBar seekBar) {
@@ -74,17 +99,36 @@ protected void onCreate(Bundle savedInstanceState) {
7499

75100
Type.Builder b = new Type.Builder(mRS, Element.RGBA_8888(mRS));
76101

77-
mOutPixelsAllocation = Allocation.createTyped(mRS, b.setX(32).setY(32).create(),
78-
Allocation.USAGE_SCRIPT |
79-
Allocation.USAGE_IO_OUTPUT);
80-
mDisplayView.setSurfaceTextureListener(this);
102+
int usage = Allocation.USAGE_SCRIPT | Allocation.USAGE_IO_OUTPUT;
81103

82-
mScript = new ScriptC_sample(mRS, getResources(), R.raw.sample);
104+
int outX = 32;
105+
int outY = 32;
106+
107+
// Wrap Linear
108+
Allocation outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
109+
TextureViewUpdater updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_LINEAR(mRS));
110+
TextureView displayView = (TextureView) findViewById(R.id.display);
111+
displayView.setSurfaceTextureListener(updater);
112+
113+
// Clamp Linear
114+
outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
115+
updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_LINEAR(mRS));
116+
displayView = (TextureView) findViewById(R.id.display2);
117+
displayView.setSurfaceTextureListener(updater);
83118

84-
mScript.set_sourceAlloc(mInPixelsAllocation);
85-
mScript.set_destAlloc(mOutPixelsAllocation);
86-
mScript.set_wrapUV(Sampler.WRAP_LINEAR(mRS));
87-
mScript.set_clampUV(Sampler.CLAMP_LINEAR(mRS));
119+
// Wrap Nearest
120+
outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
121+
updater = new TextureViewUpdater(outAlloc, Sampler.WRAP_NEAREST(mRS));
122+
displayView = (TextureView) findViewById(R.id.display3);
123+
displayView.setSurfaceTextureListener(updater);
124+
125+
// Clamp Nearest
126+
outAlloc = Allocation.createTyped(mRS, b.setX(outX).setY(outY).create(), usage);
127+
updater = new TextureViewUpdater(outAlloc, Sampler.CLAMP_NEAREST(mRS));
128+
displayView = (TextureView) findViewById(R.id.display4);
129+
displayView.setSurfaceTextureListener(updater);
130+
131+
mScript = new ScriptC_sample(mRS, getResources(), R.raw.sample);
88132
}
89133

90134
private Bitmap loadBitmap(int resource) {
@@ -98,43 +142,22 @@ private Bitmap loadBitmap(int resource) {
98142
return b2;
99143
}
100144

101-
private void filter() {
145+
private synchronized void filterAlloc(Allocation alloc, Sampler sampler) {
102146
long t = java.lang.System.currentTimeMillis();
103-
mScript.forEach_root(mOutPixelsAllocation);
104-
mOutPixelsAllocation.ioSendOutput();
147+
mScript.invoke_setSampleData(alloc, mInPixelsAllocation, sampler);
148+
mScript.forEach_root(alloc);
149+
alloc.ioSendOutput();
105150
mRS.finish();
106151
t = java.lang.System.currentTimeMillis() - t;
107152
Log.i(TAG, "Filter time is: " + t + " ms");
108153
}
109154

110155
public void benchmark(View v) {
111-
filter();
156+
/*filterAlloc();
112157
long t = java.lang.System.currentTimeMillis();
113-
filter();
158+
filterAlloc();
114159
t = java.lang.System.currentTimeMillis() - t;
115160
mDisplayView.invalidate();
116-
mBenchmarkResult.setText("Result: " + t + " ms");
117-
}
118-
119-
120-
@Override
121-
public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
122-
mOutPixelsAllocation.setSurfaceTexture(surface);
123-
filter();
124-
}
125-
126-
@Override
127-
public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
128-
mOutPixelsAllocation.setSurfaceTexture(surface);
129-
}
130-
131-
@Override
132-
public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
133-
mOutPixelsAllocation.setSurfaceTexture(null);
134-
return true;
135-
}
136-
137-
@Override
138-
public void onSurfaceTextureUpdated(SurfaceTexture surface) {
161+
mBenchmarkResult.setText("Result: " + t + " ms");*/
139162
}
140163
}

tests/RenderScriptTests/SampleTest/src/com/android/rs/sample/sample.rs

Lines changed: 60 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -18,16 +18,24 @@
1818
#pragma rs java_package_name(com.android.rs.sample)
1919
#include "rs_graphics.rsh"
2020

21-
rs_sampler wrapUV;
22-
rs_sampler clampUV;
23-
rs_allocation sourceAlloc;
24-
rs_allocation destAlloc;
21+
static rs_allocation sourceAlloc;
22+
static rs_allocation destAlloc;
23+
static rs_sampler allocSampler;
24+
25+
void setSampleData(rs_allocation dest, rs_allocation source, rs_sampler sampler) {
26+
destAlloc = dest;
27+
sourceAlloc = source;
28+
allocSampler = sampler;
29+
}
2530

26-
static uint32_t wrapI(rs_sampler_value wrap, uint32_t coord, uint32_t size) {
31+
static int32_t wrapI(rs_sampler_value wrap, int32_t coord, int32_t size) {
2732
if (wrap == RS_SAMPLER_WRAP) {
28-
return coord % (size + 1);
33+
coord = coord % size;
34+
if (coord < 0) {
35+
coord += size;
36+
}
2937
}
30-
return min(coord, size);
38+
return max(0, min(coord, size - 1));
3139
}
3240

3341
static float2 wrap(rs_sampler_value wrapS, rs_sampler_value wrapT, float2 coord) {
@@ -39,8 +47,11 @@ static float2 wrap(rs_sampler_value wrapS, rs_sampler_value wrapT, float2 coord)
3947
if (wrappedCoord.x == 0.0f && coord.x != 0.0f) {
4048
wrappedCoord.x = 1.0f;
4149
}
50+
if (wrappedCoord.x < 0.0f) {
51+
wrappedCoord.x += 1.0f;
52+
}
4253
} else {
43-
wrappedCoord.x = min(coord.x, 1.0f);
54+
wrappedCoord.x = max(0.0f, min(coord.x, 1.0f));
4455
}
4556

4657
if (wrapT == RS_SAMPLER_WRAP) {
@@ -49,14 +60,18 @@ static float2 wrap(rs_sampler_value wrapS, rs_sampler_value wrapT, float2 coord)
4960
if (wrappedCoord.y == 0.0f && coord.y != 0.0f) {
5061
wrappedCoord.y = 1.0f;
5162
}
63+
if (wrappedCoord.y < 0.0f) {
64+
wrappedCoord.y += 1.0f;
65+
}
5266
} else {
53-
wrappedCoord.y = min(coord.y, 1.0f);
67+
wrappedCoord.y = max(0.0f, min(coord.y, 1.0f));
5468
}
5569
return wrappedCoord;
5670
}
5771

5872
// Naive implementation of texture filtering for prototyping purposes
5973
static float4 sample(rs_allocation a, rs_sampler s, float2 uv) {
74+
//rsDebug("*****************************************", 0);
6075
rs_sampler_value wrapS = rsgSamplerGetWrapS(s);
6176
rs_sampler_value wrapT = rsgSamplerGetWrapT(s);
6277

@@ -65,26 +80,57 @@ static float4 sample(rs_allocation a, rs_sampler s, float2 uv) {
6580

6681
uv = wrap(wrapS, wrapT, uv);
6782

68-
uint32_t sourceW = rsAllocationGetDimX(a) - 1;
69-
uint32_t sourceH = rsAllocationGetDimY(a) - 1;
83+
int32_t sourceW = rsAllocationGetDimX(a);
84+
int32_t sourceH = rsAllocationGetDimY(a);
85+
86+
/*rsDebug("uv", uv);
87+
rsDebug("sourceW", sourceW);
88+
rsDebug("sourceH", sourceH);*/
7089

7190
float2 dimF;
7291
dimF.x = (float)(sourceW);
7392
dimF.y = (float)(sourceH);
7493
float2 pixelUV = uv * dimF;
75-
uint2 iPixel = convert_uint2(pixelUV);
94+
int2 iPixel = convert_int2(pixelUV);
95+
/*rsDebug("iPixelX initial", iPixel.x);
96+
rsDebug("iPixelY initial", iPixel.y);*/
7697

7798
if (sampleMin == RS_SAMPLER_NEAREST ||
7899
sampleMag == RS_SAMPLER_NEAREST) {
100+
iPixel.x = wrapI(wrapS, iPixel.x, sourceW);
101+
iPixel.y = wrapI(wrapT, iPixel.y, sourceH);
79102
uchar4 *nearestSample = (uchar4*)rsGetElementAt(a, iPixel.x, iPixel.y);
80103
return convert_float4(*nearestSample);
81104
}
82105

83106
float2 frac = pixelUV - convert_float2(iPixel);
107+
108+
if (frac.x < 0.5f) {
109+
iPixel.x -= 1;
110+
frac.x += 0.5f;
111+
} else {
112+
frac.x -= 0.5f;
113+
}
114+
if (frac.y < 0.5f) {
115+
iPixel.y -= 1;
116+
frac.y += 0.5f;
117+
} else {
118+
frac.y -= 0.5f;
119+
}
84120
float2 oneMinusFrac = 1.0f - frac;
85121

122+
float4 weights;
123+
weights.x = oneMinusFrac.x * oneMinusFrac.y;
124+
weights.y = frac.x * oneMinusFrac.y;
125+
weights.z = oneMinusFrac.x * frac.y;
126+
weights.w = frac.x * frac.y;
127+
86128
uint32_t nextX = wrapI(wrapS, iPixel.x + 1, sourceW);
87129
uint32_t nextY = wrapI(wrapT, iPixel.y + 1, sourceH);
130+
iPixel.x = wrapI(wrapS, iPixel.x, sourceW);
131+
iPixel.y = wrapI(wrapT, iPixel.y, sourceH);
132+
/*rsDebug("iPixelX wrapped", iPixel.x);
133+
rsDebug("iPixelY wrapped", iPixel.y);*/
88134

89135
uchar4 *p0c = (uchar4*)rsGetElementAt(a, iPixel.x, iPixel.y);
90136
uchar4 *p1c = (uchar4*)rsGetElementAt(a, nextX, iPixel.y);
@@ -96,24 +142,9 @@ static float4 sample(rs_allocation a, rs_sampler s, float2 uv) {
96142
float4 p2 = convert_float4(*p2c);
97143
float4 p3 = convert_float4(*p3c);
98144

99-
float4 weights;
100-
weights.x = oneMinusFrac.x * oneMinusFrac.y;
101-
weights.y = frac.x * oneMinusFrac.y;
102-
weights.z = oneMinusFrac.x * frac.y;
103-
weights.w = frac.x * frac.y;
104-
105145
float4 result = p0 * weights.x + p1 * weights.y + p2 * weights.z + p3 * weights.w;
106146

107-
/*rsDebug("*****************************************", 0);
108-
rsDebug("u", uv.x);
109-
rsDebug("v", uv.y);
110-
rsDebug("sourceW", sourceW);
111-
rsDebug("sourceH", sourceH);
112-
rsDebug("iPixelX", iPixel.x);
113-
rsDebug("iPixelY", iPixel.y);
114-
rsDebug("fiPixel", (float2)iPixel);
115-
rsDebug("whole", wholeUV);
116-
rsDebug("pixelUV", pixelUV);
147+
/*rsDebug("pixelUV", pixelUV);
117148
rsDebug("frac", frac);
118149
rsDebug("oneMinusFrac", oneMinusFrac);
119150
rsDebug("p0", p0);
@@ -131,15 +162,11 @@ void root(uchar4 *out, uint32_t x, uint32_t y) {
131162
float destX = (float)rsAllocationGetDimX(destAlloc) - 1.0f;
132163
float destY = (float)rsAllocationGetDimY(destAlloc) - 1.0f;
133164

134-
/*rsDebug("*****************************************", 0);
135-
rsDebug("x", x);
136-
rsDebug("y", y);*/
137-
138165
float2 uv;
139166
uv.x = (float)x / destX;
140167
uv.y = (float)y / destY;
141168

142-
out->xyz = convert_uchar3(sample(sourceAlloc, wrapUV, uv*2.0f).xyz);
169+
out->xyz = convert_uchar3(sample(sourceAlloc, allocSampler, uv*2.0f).xyz);
143170
out->w = 0xff;
144171
}
145172

0 commit comments

Comments
 (0)