blob: 4f2f52ab656a50bd391519717e667f7b97a19d15 [file] [log] [blame]
Romain Guyd7fa1222009-10-09 16:05:25 -07001/*
2 * Copyright (C) 2009 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 android.app.Activity;
20import android.os.Bundle;
21import android.graphics.BitmapFactory;
22import android.graphics.Bitmap;
23import android.graphics.Canvas;
24import android.renderscript.ScriptC;
25import android.renderscript.RenderScript;
26import android.renderscript.Type;
27import android.renderscript.Allocation;
28import android.renderscript.Element;
29import android.renderscript.Script;
30import android.view.SurfaceView;
31import android.view.SurfaceHolder;
32import android.widget.ImageView;
33import android.widget.SeekBar;
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070034import android.widget.TextView;
35import android.view.View;
Jason Sams586f3b52010-02-10 18:07:37 -080036import java.lang.Math;
Romain Guyd7fa1222009-10-09 16:05:25 -070037
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070038public class ImageProcessingActivity extends Activity
39 implements SurfaceHolder.Callback,
40 SeekBar.OnSeekBarChangeListener {
Jason Sams4d339932010-05-11 14:03:58 -070041 private Bitmap mBitmapIn;
42 private Bitmap mBitmapOut;
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070043 private Bitmap mBitmapScratch;
Stephen Hines93a958f2010-09-16 17:18:55 -070044 private ScriptC_threshold mScript;
45 private ScriptC_vertical_blur mScriptVBlur;
46 private ScriptC_horizontal_blur mScriptHBlur;
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070047 private int mRadius = 0;
48 private SeekBar mRadiusSeekBar;
49
50 private float mInBlack = 0.0f;
51 private SeekBar mInBlackSeekBar;
52 private float mOutBlack = 0.0f;
53 private SeekBar mOutBlackSeekBar;
54 private float mInWhite = 255.0f;
55 private SeekBar mInWhiteSeekBar;
56 private float mOutWhite = 255.0f;
57 private SeekBar mOutWhiteSeekBar;
58 private float mGamma = 1.0f;
59 private SeekBar mGammaSeekBar;
60
61 private float mSaturation = 1.0f;
62 private SeekBar mSaturationSeekBar;
63
64 private TextView mBenchmarkResult;
Romain Guyd7fa1222009-10-09 16:05:25 -070065
66 @SuppressWarnings({"FieldCanBeLocal"})
67 private RenderScript mRS;
68 @SuppressWarnings({"FieldCanBeLocal"})
Romain Guyd7fa1222009-10-09 16:05:25 -070069 private Type mPixelType;
70 @SuppressWarnings({"FieldCanBeLocal"})
71 private Allocation mInPixelsAllocation;
72 @SuppressWarnings({"FieldCanBeLocal"})
73 private Allocation mOutPixelsAllocation;
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070074 @SuppressWarnings({"FieldCanBeLocal"})
Jason Sams43c31422010-08-16 12:29:23 -070075 private Allocation mScratchPixelsAllocation1;
76 private Allocation mScratchPixelsAllocation2;
Romain Guyd7fa1222009-10-09 16:05:25 -070077
78 private SurfaceView mSurfaceView;
79 private ImageView mDisplayView;
80
Jason Samsbf6ef8d2010-12-06 15:59:59 -080081 class FilterCallback extends RenderScript.RSMessageHandler {
Romain Guyd7fa1222009-10-09 16:05:25 -070082 private Runnable mAction = new Runnable() {
83 public void run() {
Romain Guyd7fa1222009-10-09 16:05:25 -070084 mDisplayView.invalidate();
85 }
Jason Sams718cd1f2009-12-23 14:35:29 -080086 };
Romain Guyd7fa1222009-10-09 16:05:25 -070087
88 @Override
89 public void run() {
90 mSurfaceView.removeCallbacks(mAction);
91 mSurfaceView.post(mAction);
92 }
93 }
94
Jason Sams4d339932010-05-11 14:03:58 -070095 int in[];
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070096 int interm[];
Jason Sams4d339932010-05-11 14:03:58 -070097 int out[];
Alex Sakhartchouk814326b2010-05-19 16:28:27 -070098 int MAX_RADIUS = 25;
99 // Store our coefficients here
100 float gaussian[];
101
102 private long javaFilter() {
103 final int width = mBitmapIn.getWidth();
104 final int height = mBitmapIn.getHeight();
105 final int count = width * height;
Jason Sams586f3b52010-02-10 18:07:37 -0800106
Jason Sams4d339932010-05-11 14:03:58 -0700107 if (in == null) {
108 in = new int[count];
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700109 interm = new int[count];
Jason Sams4d339932010-05-11 14:03:58 -0700110 out = new int[count];
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700111 gaussian = new float[MAX_RADIUS * 2 + 1];
112 mBitmapIn.getPixels(in, 0, width, 0, 0, width, height);
Jason Sams586f3b52010-02-10 18:07:37 -0800113 }
114
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700115 long t = java.lang.System.currentTimeMillis();
Jason Sams586f3b52010-02-10 18:07:37 -0800116
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700117 int w, h, r;
118
119 float fRadius = (float)mRadius;
120 int radius = (int)mRadius;
121
122 // Compute gaussian weights for the blur
123 // e is the euler's number
124 float e = 2.718281828459045f;
125 float pi = 3.1415926535897932f;
126 // g(x) = ( 1 / sqrt( 2 * pi ) * sigma) * e ^ ( -x^2 / 2 * sigma^2 )
127 // x is of the form [-radius .. 0 .. radius]
128 // and sigma varies with radius.
129 // Based on some experimental radius values and sigma's
130 // we approximately fit sigma = f(radius) as
131 // sigma = radius * 0.4 + 0.6
132 // The larger the radius gets, the more our gaussian blur
133 // will resemble a box blur since with large sigma
134 // the gaussian curve begins to lose its shape
135 float sigma = 0.4f * fRadius + 0.6f;
136 // Now compute the coefficints
137 // We will store some redundant values to save some math during
138 // the blur calculations
139 // precompute some values
140 float coeff1 = 1.0f / (float)(Math.sqrt( 2.0f * pi ) * sigma);
141 float coeff2 = - 1.0f / (2.0f * sigma * sigma);
142 float normalizeFactor = 0.0f;
143 float floatR = 0.0f;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800144 for (r = -radius; r <= radius; r ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700145 floatR = (float)r;
146 gaussian[r + radius] = coeff1 * (float)Math.pow(e, floatR * floatR * coeff2);
147 normalizeFactor += gaussian[r + radius];
148 }
149
150 //Now we need to normalize the weights because all our coefficients need to add up to one
151 normalizeFactor = 1.0f / normalizeFactor;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800152 for (r = -radius; r <= radius; r ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700153 floatR = (float)r;
154 gaussian[r + radius] *= normalizeFactor;
155 }
156
157 float blurredPixelR = 0.0f;
158 float blurredPixelG = 0.0f;
159 float blurredPixelB = 0.0f;
160 float blurredPixelA = 0.0f;
161
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800162 for (h = 0; h < height; h ++) {
163 for (w = 0; w < width; w ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700164
165 blurredPixelR = 0.0f;
166 blurredPixelG = 0.0f;
167 blurredPixelB = 0.0f;
168 blurredPixelA = 0.0f;
169
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800170 for (r = -radius; r <= radius; r ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700171 // Stepping left and right away from the pixel
172 int validW = w + r;
173 // Clamp to zero and width max() isn't exposed for ints yet
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800174 if (validW < 0) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700175 validW = 0;
176 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800177 if (validW > width - 1) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700178 validW = width - 1;
179 }
180
181 int input = in[h*width + validW];
182
183 int R = ((input >> 24) & 0xff);
184 int G = ((input >> 16) & 0xff);
185 int B = ((input >> 8) & 0xff);
186 int A = (input & 0xff);
187
188 float weight = gaussian[r + radius];
189
190 blurredPixelR += (float)(R)*weight;
191 blurredPixelG += (float)(G)*weight;
192 blurredPixelB += (float)(B)*weight;
193 blurredPixelA += (float)(A)*weight;
194 }
195
196 int R = (int)blurredPixelR;
197 int G = (int)blurredPixelG;
198 int B = (int)blurredPixelB;
199 int A = (int)blurredPixelA;
200
201 interm[h*width + w] = (R << 24) | (G << 16) | (B << 8) | (A);
Jason Sams4d339932010-05-11 14:03:58 -0700202 }
203 }
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700204
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800205 for (h = 0; h < height; h ++) {
206 for (w = 0; w < width; w ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700207
208 blurredPixelR = 0.0f;
209 blurredPixelG = 0.0f;
210 blurredPixelB = 0.0f;
211 blurredPixelA = 0.0f;
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800212 for (r = -radius; r <= radius; r ++) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700213 int validH = h + r;
214 // Clamp to zero and width
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800215 if (validH < 0) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700216 validH = 0;
217 }
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800218 if (validH > height - 1) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700219 validH = height - 1;
220 }
221
222 int input = interm[validH*width + w];
223
224 int R = ((input >> 24) & 0xff);
225 int G = ((input >> 16) & 0xff);
226 int B = ((input >> 8) & 0xff);
227 int A = (input & 0xff);
228
229 float weight = gaussian[r + radius];
230
231 blurredPixelR += (float)(R)*weight;
232 blurredPixelG += (float)(G)*weight;
233 blurredPixelB += (float)(B)*weight;
234 blurredPixelA += (float)(A)*weight;
235 }
236
237 int R = (int)blurredPixelR;
238 int G = (int)blurredPixelG;
239 int B = (int)blurredPixelB;
240 int A = (int)blurredPixelA;
241
242 out[h*width + w] = (R << 24) | (G << 16) | (B << 8) | (A);
243 }
244 }
245
246 t = java.lang.System.currentTimeMillis() - t;
247 android.util.Log.v("Img", "Java frame time ms " + t);
248 mBitmapOut.setPixels(out, 0, width, 0, 0, width, height);
249 return t;
250 }
251
252 public void onProgressChanged(SeekBar seekBar, int progress, boolean fromUser) {
253 if (fromUser) {
254
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800255 if (seekBar == mRadiusSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700256 float fRadius = progress / 100.0f;
257 fRadius *= (float)(MAX_RADIUS);
258 mRadius = (int)fRadius;
259
260 mScript.set_radius(mRadius);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800261 } else if (seekBar == mInBlackSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700262 mInBlack = (float)progress;
Jason Sams43c31422010-08-16 12:29:23 -0700263 mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800264 } else if (seekBar == mOutBlackSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700265 mOutBlack = (float)progress;
Jason Sams43c31422010-08-16 12:29:23 -0700266 mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800267 } else if (seekBar == mInWhiteSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700268 mInWhite = (float)progress + 127.0f;
Jason Sams43c31422010-08-16 12:29:23 -0700269 mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800270 } else if (seekBar == mOutWhiteSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700271 mOutWhite = (float)progress + 127.0f;
Jason Sams43c31422010-08-16 12:29:23 -0700272 mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800273 } else if (seekBar == mGammaSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700274 mGamma = (float)progress/100.0f;
275 mGamma = Math.max(mGamma, 0.1f);
276 mGamma = 1.0f / mGamma;
Jason Sams43c31422010-08-16 12:29:23 -0700277 mScriptVBlur.invoke_setGamma(mGamma);
Alex Sakhartchouked9f2102010-11-09 17:00:54 -0800278 } else if (seekBar == mSaturationSeekBar) {
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700279 mSaturation = (float)progress / 50.0f;
Jason Sams43c31422010-08-16 12:29:23 -0700280 mScriptVBlur.invoke_setSaturation(mSaturation);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700281 }
282
283 long t = java.lang.System.currentTimeMillis();
284 if (true) {
Jason Samsf110d4b2010-06-21 17:42:41 -0700285 mScript.invoke_filter();
Jason Sams4ef66502010-12-10 16:03:15 -0800286 mOutPixelsAllocation.copyTo(mBitmapOut);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700287 } else {
288 javaFilter();
Jason Samsf0690c42010-07-29 17:31:14 -0700289 mDisplayView.invalidate();
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700290 }
291
292 t = java.lang.System.currentTimeMillis() - t;
293 android.util.Log.v("Img", "Renderscript frame time core ms " + t);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700294 }
295 }
296
297 public void onStartTrackingTouch(SeekBar seekBar) {
298 }
299
300 public void onStopTrackingTouch(SeekBar seekBar) {
Jason Sams586f3b52010-02-10 18:07:37 -0800301 }
302
Romain Guyd7fa1222009-10-09 16:05:25 -0700303 @Override
304 protected void onCreate(Bundle savedInstanceState) {
305 super.onCreate(savedInstanceState);
306 setContentView(R.layout.main);
307
Jason Sams4d339932010-05-11 14:03:58 -0700308 mBitmapIn = loadBitmap(R.drawable.data);
309 mBitmapOut = loadBitmap(R.drawable.data);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700310 mBitmapScratch = loadBitmap(R.drawable.data);
Romain Guyd7fa1222009-10-09 16:05:25 -0700311
312 mSurfaceView = (SurfaceView) findViewById(R.id.surface);
313 mSurfaceView.getHolder().addCallback(this);
314
315 mDisplayView = (ImageView) findViewById(R.id.display);
Jason Sams4d339932010-05-11 14:03:58 -0700316 mDisplayView.setImageBitmap(mBitmapOut);
Romain Guyd7fa1222009-10-09 16:05:25 -0700317
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700318 mRadiusSeekBar = (SeekBar) findViewById(R.id.radius);
319 mRadiusSeekBar.setOnSeekBarChangeListener(this);
Jason Sams586f3b52010-02-10 18:07:37 -0800320
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700321 mInBlackSeekBar = (SeekBar)findViewById(R.id.inBlack);
322 mInBlackSeekBar.setOnSeekBarChangeListener(this);
323 mInBlackSeekBar.setMax(128);
324 mInBlackSeekBar.setProgress(0);
325 mOutBlackSeekBar = (SeekBar)findViewById(R.id.outBlack);
326 mOutBlackSeekBar.setOnSeekBarChangeListener(this);
327 mOutBlackSeekBar.setMax(128);
328 mOutBlackSeekBar.setProgress(0);
Romain Guyd7fa1222009-10-09 16:05:25 -0700329
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700330 mInWhiteSeekBar = (SeekBar)findViewById(R.id.inWhite);
331 mInWhiteSeekBar.setOnSeekBarChangeListener(this);
332 mInWhiteSeekBar.setMax(128);
333 mInWhiteSeekBar.setProgress(128);
334 mOutWhiteSeekBar = (SeekBar)findViewById(R.id.outWhite);
335 mOutWhiteSeekBar.setOnSeekBarChangeListener(this);
336 mOutWhiteSeekBar.setMax(128);
337 mOutWhiteSeekBar.setProgress(128);
Romain Guyd7fa1222009-10-09 16:05:25 -0700338
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700339 mGammaSeekBar = (SeekBar)findViewById(R.id.inGamma);
340 mGammaSeekBar.setOnSeekBarChangeListener(this);
341 mGammaSeekBar.setMax(150);
342 mGammaSeekBar.setProgress(100);
343
344 mSaturationSeekBar = (SeekBar)findViewById(R.id.inSaturation);
345 mSaturationSeekBar.setOnSeekBarChangeListener(this);
346 mSaturationSeekBar.setProgress(50);
347
348 mBenchmarkResult = (TextView) findViewById(R.id.benchmarkText);
Alex Sakhartchoukdb217e02011-02-17 14:43:27 -0800349 mBenchmarkResult.setText("Result: not run");
Romain Guyd7fa1222009-10-09 16:05:25 -0700350 }
351
352 public void surfaceCreated(SurfaceHolder holder) {
Jason Sams4d339932010-05-11 14:03:58 -0700353 createScript();
Jason Samsf110d4b2010-06-21 17:42:41 -0700354 mScript.invoke_filter();
Jason Sams4ef66502010-12-10 16:03:15 -0800355 mOutPixelsAllocation.copyTo(mBitmapOut);
Romain Guyd7fa1222009-10-09 16:05:25 -0700356 }
357
358 public void surfaceChanged(SurfaceHolder holder, int format, int width, int height) {
359 }
360
361 public void surfaceDestroyed(SurfaceHolder holder) {
362 }
Jason Sams718cd1f2009-12-23 14:35:29 -0800363
Jason Sams4d339932010-05-11 14:03:58 -0700364 private void createScript() {
Shih-wei Liao6b32fab2010-12-10 01:03:59 -0800365 mRS = RenderScript.create(this);
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800366 mRS.setMessageHandler(new FilterCallback());
Romain Guyd7fa1222009-10-09 16:05:25 -0700367
Jason Sams4ef66502010-12-10 16:03:15 -0800368 mInPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapIn,
369 Allocation.MipmapControl.MIPMAP_NONE,
370 Allocation.USAGE_SCRIPT);
371 mOutPixelsAllocation = Allocation.createFromBitmap(mRS, mBitmapOut,
372 Allocation.MipmapControl.MIPMAP_NONE,
373 Allocation.USAGE_SCRIPT);
Jason Sams43c31422010-08-16 12:29:23 -0700374
375 Type.Builder tb = new Type.Builder(mRS, Element.F32_4(mRS));
Jason Samsbf6ef8d2010-12-06 15:59:59 -0800376 tb.setX(mBitmapIn.getWidth());
377 tb.setY(mBitmapIn.getHeight());
Jason Sams43c31422010-08-16 12:29:23 -0700378 mScratchPixelsAllocation1 = Allocation.createTyped(mRS, tb.create());
379 mScratchPixelsAllocation2 = Allocation.createTyped(mRS, tb.create());
Romain Guyd7fa1222009-10-09 16:05:25 -0700380
Jason Sams3ba02b32010-11-03 23:01:38 -0700381 mScriptVBlur = new ScriptC_vertical_blur(mRS, getResources(), R.raw.vertical_blur);
382 mScriptHBlur = new ScriptC_horizontal_blur(mRS, getResources(), R.raw.horizontal_blur);
Jason Sams8f8a5722010-07-15 17:11:13 -0700383
Jason Sams3ba02b32010-11-03 23:01:38 -0700384 mScript = new ScriptC_threshold(mRS, getResources(), R.raw.threshold);
Jason Sams4d339932010-05-11 14:03:58 -0700385 mScript.set_width(mBitmapIn.getWidth());
386 mScript.set_height(mBitmapIn.getHeight());
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700387 mScript.set_radius(mRadius);
388
Jason Sams43c31422010-08-16 12:29:23 -0700389 mScriptVBlur.invoke_setLevels(mInBlack, mOutBlack, mInWhite, mOutWhite);
390 mScriptVBlur.invoke_setGamma(mGamma);
391 mScriptVBlur.invoke_setSaturation(mSaturation);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700392
Jason Sams4d339932010-05-11 14:03:58 -0700393 mScript.bind_InPixel(mInPixelsAllocation);
394 mScript.bind_OutPixel(mOutPixelsAllocation);
Jason Sams43c31422010-08-16 12:29:23 -0700395 mScript.bind_ScratchPixel1(mScratchPixelsAllocation1);
396 mScript.bind_ScratchPixel2(mScratchPixelsAllocation2);
Jason Sams8f8a5722010-07-15 17:11:13 -0700397
398 mScript.set_vBlurScript(mScriptVBlur);
399 mScript.set_hBlurScript(mScriptHBlur);
Romain Guyd7fa1222009-10-09 16:05:25 -0700400 }
401
402 private Bitmap loadBitmap(int resource) {
403 final BitmapFactory.Options options = new BitmapFactory.Options();
404 options.inPreferredConfig = Bitmap.Config.ARGB_8888;
405 return copyBitmap(BitmapFactory.decodeResource(getResources(), resource, options));
406 }
407
408 private static Bitmap copyBitmap(Bitmap source) {
409 Bitmap b = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());
410 Canvas c = new Canvas(b);
411 c.drawBitmap(source, 0, 0, null);
412 source.recycle();
413 return b;
414 }
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700415
416 // button hook
417 public void benchmark(View v) {
418 android.util.Log.v("Img", "Benchmarking");
419 int oldRadius = mRadius;
420 mRadius = MAX_RADIUS;
421 mScript.set_radius(mRadius);
422
423 long t = java.lang.System.currentTimeMillis();
424
Jason Sams43c31422010-08-16 12:29:23 -0700425 mScript.invoke_filter();
Jason Sams4ef66502010-12-10 16:03:15 -0800426 mOutPixelsAllocation.copyTo(mBitmapOut);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700427
428 t = java.lang.System.currentTimeMillis() - t;
429 android.util.Log.v("Img", "Renderscript frame time core ms " + t);
430
Jason Sams43c31422010-08-16 12:29:23 -0700431 //long javaTime = javaFilter();
432 //mBenchmarkResult.setText("RS: " + t + " ms Java: " + javaTime + " ms");
Alex Sakhartchoukdb217e02011-02-17 14:43:27 -0800433 mBenchmarkResult.setText("Result: " + t + " ms");
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700434
435 mRadius = oldRadius;
436 mScript.set_radius(mRadius);
437
Jason Samsf110d4b2010-06-21 17:42:41 -0700438 mScript.invoke_filter();
Jason Sams4ef66502010-12-10 16:03:15 -0800439 mOutPixelsAllocation.copyTo(mBitmapOut);
Alex Sakhartchouk814326b2010-05-19 16:28:27 -0700440 }
Romain Guyd7fa1222009-10-09 16:05:25 -0700441}