Skip to content

Commit e65afdb

Browse files
Jason SamsAndroid (Google) Code Review
authored andcommitted
Merge "Lighten grain and make live preview gpu friendly." into jb-mr1-dev
2 parents a6af5fc + 1ebb720 commit e65afdb

File tree

5 files changed

+43
-44
lines changed

5 files changed

+43
-44
lines changed

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

Lines changed: 25 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -45,19 +45,40 @@ public void onBar1Changed(int progress) {
4545
mScript.set_gNoiseStrength(s);
4646
}
4747

48+
private int findHighBit(int v) {
49+
int bit = 0;
50+
while (v > 1) {
51+
bit++;
52+
v >>= 1;
53+
}
54+
return bit;
55+
}
56+
57+
4858
public void createTest(android.content.res.Resources res) {
4959
int width = mInPixelsAllocation.getType().getX();
5060
int height = mInPixelsAllocation.getType().getY();
5161

62+
int noiseW = findHighBit(width);
63+
int noiseH = findHighBit(height);
64+
if (noiseW > 9) {
65+
noiseW = 9;
66+
}
67+
if (noiseH > 9) {
68+
noiseH = 9;
69+
}
70+
noiseW = 1 << noiseW;
71+
noiseH = 1 << noiseH;
72+
5273
Type.Builder tb = new Type.Builder(mRS, Element.U8(mRS));
53-
tb.setX(width);
54-
tb.setY(height);
74+
tb.setX(noiseW);
75+
tb.setY(noiseH);
5576
mNoise = Allocation.createTyped(mRS, tb.create());
5677
mNoise2 = Allocation.createTyped(mRS, tb.create());
5778

5879
mScript = new ScriptC_grain(mRS, res, R.raw.grain);
59-
mScript.set_gWidth(width);
60-
mScript.set_gHeight(height);
80+
mScript.set_gWMask(noiseW - 1);
81+
mScript.set_gHMask(noiseH - 1);
6182
mScript.set_gNoiseStrength(0.5f);
6283
mScript.set_gBlendSource(mNoise);
6384
mScript.set_gNoise(mNoise2);

tests/RenderScriptTests/ImageProcessing/src/com/android/rs/image/grain.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -38,15 +38,15 @@ void genRand(uchar *out) {
3838
* 1 2 1
3939
*/
4040

41-
int32_t gWidth;
42-
int32_t gHeight;
41+
int32_t gWMask;
42+
int32_t gHMask;
4343

4444
rs_allocation gBlendSource;
4545
void blend9(uchar *out, uint32_t x, uint32_t y) {
46-
uint32_t x1 = min((int32_t)x+1, (int32_t)(gWidth -1));
47-
uint32_t x2 = max((int32_t)x-1, (int32_t)0);
48-
uint32_t y1 = min((int32_t)y+1, (int32_t)(gHeight -1));
49-
uint32_t y2 = max((int32_t)y-1, (int32_t)0);
46+
uint32_t x1 = (x-1) & gWMask;
47+
uint32_t x2 = (x+1) & gWMask;
48+
uint32_t y1 = (y-1) & gHMask;
49+
uint32_t y2 = (y+1) & gHMask;
5050

5151
uint p00 = 56 * rsGetElementAt_uchar(gBlendSource, x1, y1);
5252
uint p01 = 114 * rsGetElementAt_uchar(gBlendSource, x, y1);
@@ -78,7 +78,7 @@ float gNoiseStrength;
7878
rs_allocation gNoise;
7979
void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
8080
float4 ip = convert_float4(*in);
81-
float pnoise = (float) rsGetElementAt_uchar(gNoise, x, y);
81+
float pnoise = (float) rsGetElementAt_uchar(gNoise, x & gWMask, y & gHMask);
8282

8383
float energy_level = ip.r + ip.g + ip.b;
8484
float energy_mask = (28.f - sqrt(energy_level)) * 0.03571f;

tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -305,8 +305,7 @@ protected Boolean doInBackground(byte[]... datas) {
305305

306306
long t1 = java.lang.System.currentTimeMillis();
307307

308-
mFilterYuv.execute(data);
309-
mFilterYuv.copyOut(mCallbackBitmap);
308+
mFilterYuv.execute(data, mCallbackBitmap);
310309

311310
long t2 = java.lang.System.currentTimeMillis();
312311
mTiming[mTimingSlot++] = t2 - t1;

tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java

Lines changed: 7 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -42,6 +42,7 @@ public class RsYuv
4242
private Allocation mAllocationOut;
4343
private Allocation mAllocationIn;
4444
private ScriptC_yuv mScript;
45+
private ScriptIntrinsicYuvToRGB mYuv;
4546

4647
RsYuv(RenderScript rs, Resources res, int width, int height) {
4748
mHeight = height;
@@ -50,6 +51,8 @@ public class RsYuv
5051
mScript = new ScriptC_yuv(mRS, res, R.raw.yuv);
5152
mScript.invoke_setSize(mWidth, mHeight);
5253

54+
mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
55+
5356
Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
5457
tb.setX(mWidth);
5558
tb.setY(mHeight);
@@ -58,34 +61,16 @@ public class RsYuv
5861
mAllocationIn = Allocation.createSized(rs, Element.U8(mRS), (mHeight * mWidth) +
5962
((mHeight / 2) * (mWidth / 2) * 2));
6063

61-
mScript.bind_gYuvIn(mAllocationIn);
64+
mYuv.setInput(mAllocationIn);
6265
}
6366

6467
private long mTiming[] = new long[50];
6568
private int mTimingSlot = 0;
6669

67-
void execute(byte[] yuv) {
70+
void execute(byte[] yuv, Bitmap b) {
6871
mAllocationIn.copyFrom(yuv);
69-
mRS.finish();
70-
71-
long t1 = java.lang.System.currentTimeMillis();
72-
mScript.forEach_root(mAllocationOut);
73-
mRS.finish();
74-
long t2 = java.lang.System.currentTimeMillis();
75-
76-
mTiming[mTimingSlot++] = t2 - t1;
77-
if (mTimingSlot >= mTiming.length) {
78-
float total = 0;
79-
for (int i=0; i<mTiming.length; i++) {
80-
total += (float)mTiming[i];
81-
}
82-
total /= mTiming.length;
83-
Log.e("yuv", "core time = " + total);
84-
mTimingSlot = 0;
85-
}
86-
}
87-
88-
void copyOut(Bitmap b) {
72+
mYuv.forEach(mAllocationOut);
73+
mScript.forEach_root(mAllocationOut, mAllocationOut);
8974
mAllocationOut.copyTo(b);
9075
}
9176

tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs

Lines changed: 3 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -3,8 +3,6 @@
33
#pragma rs java_package_name(com.android.rs.livepreview)
44
#pragma rs_fp_relaxed
55

6-
uchar *gYuvIn;
7-
86
static int gWidth;
97
static int gHeight;
108
static uchar crossProcess_tableR[256];
@@ -80,13 +78,9 @@ static uchar4 vignette(uchar4 color, uint32_t x, uint32_t y) {
8078
return convert_uchar4(c);
8179
}
8280

83-
void root(uchar4 *out, uint32_t x, uint32_t y) {
84-
uchar Y = gYuvIn[(y * gWidth) + x];
85-
uchar *uv = &gYuvIn[gWidth * gHeight];
86-
uv += (((x>>1)<<1) + (y>>1) * gWidth);
87-
88-
uchar4 p = rsYuvToRGBA_uchar4(Y, uv[1], uv[0]);
89-
p = crossProcess_i(p);
81+
void root(const uchar4 *in, uchar4 *out, uint32_t x, uint32_t y) {
82+
uchar4 p;
83+
p = crossProcess_i(*in);
9084
p = vignette(p, x, y);
9185

9286
out->rgba = p;

0 commit comments

Comments
 (0)