OSDN Git Service

Convert live preview demo to IO_OUTPUT
authorJason Sams <jsams@google.com>
Fri, 1 Feb 2013 20:48:58 +0000 (12:48 -0800)
committerJason Sams <jsams@google.com>
Thu, 14 Feb 2013 00:07:50 +0000 (16:07 -0800)
Change-Id: I01193932a6fcd572841434a59d34eda60d9b8b02

Fix aspect ratio

tests/RenderScriptTests/LivePreview/res/layout/cf_main.xml
tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/CameraPreviewActivity.java
tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/RsYuv.java
tests/RenderScriptTests/LivePreview/src/com/android/rs/livepreview/yuv.rs

index c7dcca5..ecb736b 100644 (file)
@@ -53,7 +53,7 @@
             android:layout_height="fill_parent"
             android:layout_weight="3" >
 
-            <ImageView
+            <TextureView
                 android:id="@+id/format_view"
                 android:layout_height="0dp"
                 android:layout_width="fill_parent"
index 89eec2c..62dcaa8 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -65,7 +65,7 @@ public class CameraPreviewActivity extends Activity
     private int mPreviewTexWidth;
     private int mPreviewTexHeight;
 
-    private ImageView mFormatView;
+    //private TextureView mFormatView;
 
     private Spinner mCameraSpinner;
     private Spinner mResolutionSpinner;
@@ -77,7 +77,8 @@ public class CameraPreviewActivity extends Activity
     private Camera.Size mNextPreviewSize;
     private Camera.Size mPreviewSize;
 
-    private Bitmap mCallbackBitmap;
+    private TextureView mOutputView;
+    //private Bitmap mCallbackBitmap;
 
     private static final int STATE_OFF = 0;
     private static final int STATE_PREVIEW = 1;
@@ -97,7 +98,7 @@ public class CameraPreviewActivity extends Activity
         setContentView(R.layout.cf_main);
 
         mPreviewView = (TextureView) findViewById(R.id.preview_view);
-        mFormatView = (ImageView) findViewById(R.id.format_view);
+        mOutputView = (TextureView) findViewById(R.id.format_view);
 
         mPreviewView.setSurfaceTextureListener(this);
 
@@ -115,8 +116,9 @@ public class CameraPreviewActivity extends Activity
         mResolutionSpinner = (Spinner) findViewById(R.id.resolution_selection);
         mResolutionSpinner.setOnItemSelectedListener(mResolutionSelectedListener);
 
-
         mRS = RenderScript.create(this);
+        mFilterYuv = new RsYuv(mRS);
+        mOutputView.setSurfaceTextureListener(mFilterYuv);
     }
 
     @Override
@@ -227,8 +229,8 @@ public class CameraPreviewActivity extends Activity
 
         // Set initial values
 
-        mNextPreviewSize = mPreviewSizes.get(0);
-        mResolutionSpinner.setSelection(0);
+        mNextPreviewSize = mPreviewSizes.get(15);
+        mResolutionSpinner.setSelection(15);
 
         if (mPreviewTexture != null) {
             startPreview();
@@ -271,6 +273,7 @@ public class CameraPreviewActivity extends Activity
                 mPreviewTexHeight * (1 - heightRatio/widthRatio)/2);
 
         mPreviewView.setTransform(transform);
+        mOutputView.setTransform(transform);
 
         mPreviewSize   = mNextPreviewSize;
 
@@ -305,7 +308,7 @@ public class CameraPreviewActivity extends Activity
 
             long t1 = java.lang.System.currentTimeMillis();
 
-            mFilterYuv.execute(data, mCallbackBitmap);
+            mFilterYuv.execute(data);
 
             long t2 = java.lang.System.currentTimeMillis();
             mTiming[mTimingSlot++] = t2 - t1;
@@ -325,7 +328,7 @@ public class CameraPreviewActivity extends Activity
         }
 
         protected void onPostExecute(Boolean result) {
-            mFormatView.invalidate();
+            mOutputView.invalidate();
         }
 
     }
@@ -355,21 +358,13 @@ public class CameraPreviewActivity extends Activity
 
         mProcessInProgress = true;
 
-        if (mCallbackBitmap == null ||
-                mPreviewSize.width != mCallbackBitmap.getWidth() ||
-                mPreviewSize.height != mCallbackBitmap.getHeight() ) {
-            mCallbackBitmap =
-                    Bitmap.createBitmap(
-                        mPreviewSize.width, mPreviewSize.height,
-                        Bitmap.Config.ARGB_8888);
-            mFilterYuv = new RsYuv(mRS, getResources(), mPreviewSize.width, mPreviewSize.height);
-            mFormatView.setImageBitmap(mCallbackBitmap);
-        }
-
+        if ((mFilterYuv == null) ||
+            (mPreviewSize.width != mFilterYuv.getWidth()) ||
+            (mPreviewSize.height != mFilterYuv.getHeight()) ) {
 
-        mFormatView.invalidate();
+            mFilterYuv.reset(mPreviewSize.width, mPreviewSize.height);
+        }
 
-        mCamera.addCallbackBuffer(data);
         mProcessInProgress = true;
         new ProcessPreviewDataTask().execute(data);
     }
index 978ae12..4d1627d 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (C) 2012 The Android Open Source Project
+ * Copyright (C) 2013 The Android Open Source Project
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -34,7 +34,7 @@ import android.renderscript.*;
 
 import android.graphics.Bitmap;
 
-public class RsYuv
+public class RsYuv implements TextureView.SurfaceTextureListener
 {
     private int mHeight;
     private int mWidth;
@@ -43,36 +43,104 @@ public class RsYuv
     private Allocation mAllocationIn;
     private ScriptC_yuv mScript;
     private ScriptIntrinsicYuvToRGB mYuv;
+    private boolean mHaveSurface;
+    private SurfaceTexture mSurface;
+    private ScriptGroup mGroup;
 
-    RsYuv(RenderScript rs, Resources res, int width, int height) {
+    RsYuv(RenderScript rs) {
+        mRS = rs;
+        mScript = new ScriptC_yuv(mRS);
+        mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
+    }
+
+    void setupSurface() {
+        if (mAllocationOut != null) {
+            mAllocationOut.setSurfaceTexture(mSurface);
+        }
+        if (mSurface != null) {
+            mHaveSurface = true;
+        } else {
+            mHaveSurface = false;
+        }
+    }
+
+    void reset(int width, int height) {
+        if (mAllocationOut != null) {
+            mAllocationOut.destroy();
+        }
+
+        android.util.Log.v("cpa", "reset " + width + ", " + height);
         mHeight = height;
         mWidth = width;
-        mRS = rs;
-        mScript = new ScriptC_yuv(mRS, res, R.raw.yuv);
         mScript.invoke_setSize(mWidth, mHeight);
 
-        mYuv = ScriptIntrinsicYuvToRGB.create(rs, Element.RGBA_8888(mRS));
-
         Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
         tb.setX(mWidth);
         tb.setY(mHeight);
-
-        mAllocationOut = Allocation.createTyped(rs, tb.create());
-        mAllocationIn = Allocation.createSized(rs, Element.U8(mRS), (mHeight * mWidth) +
+        Type t = tb.create();
+        mAllocationOut = Allocation.createTyped(mRS, t, Allocation.USAGE_SCRIPT |
+                                                        Allocation.USAGE_IO_OUTPUT);
+        mAllocationIn = Allocation.createSized(mRS, Element.U8(mRS), (mHeight * mWidth) +
                                                ((mHeight / 2) * (mWidth / 2) * 2));
-
         mYuv.setInput(mAllocationIn);
+        setupSurface();
+
+
+        ScriptGroup.Builder b = new ScriptGroup.Builder(mRS);
+        b.addKernel(mScript.getKernelID_root());
+        b.addKernel(mYuv.getKernelID());
+        b.addConnection(t, mYuv.getKernelID(), mScript.getKernelID_root());
+        mGroup = b.create();
+    }
+
+    public int getWidth() {
+        return mWidth;
+    }
+    public int getHeight() {
+        return mHeight;
     }
 
     private long mTiming[] = new long[50];
     private int mTimingSlot = 0;
 
-    void execute(byte[] yuv, Bitmap b) {
+    void execute(byte[] yuv) {
         mAllocationIn.copyFrom(yuv);
-        mYuv.forEach(mAllocationOut);
-        mScript.forEach_root(mAllocationOut, mAllocationOut);
-        mAllocationOut.copyTo(b);
+        if (mHaveSurface) {
+            mGroup.setOutput(mScript.getKernelID_root(), mAllocationOut);
+            mGroup.execute();
+
+            //mYuv.forEach(mAllocationOut);
+            //mScript.forEach_root(mAllocationOut, mAllocationOut);
+            mAllocationOut.ioSendOutput();
+        }
     }
 
+
+
+    @Override
+    public void onSurfaceTextureAvailable(SurfaceTexture surface, int width, int height) {
+        android.util.Log.v("cpa", "onSurfaceTextureAvailable " + surface);
+        mSurface = surface;
+        setupSurface();
+    }
+
+    @Override
+    public void onSurfaceTextureSizeChanged(SurfaceTexture surface, int width, int height) {
+        android.util.Log.v("cpa", "onSurfaceTextureSizeChanged " + surface);
+        mSurface = surface;
+        setupSurface();
+    }
+
+    @Override
+    public boolean onSurfaceTextureDestroyed(SurfaceTexture surface) {
+        android.util.Log.v("cpa", "onSurfaceTextureDestroyed " + surface);
+        mSurface = surface;
+        setupSurface();
+        return true;
+    }
+
+    @Override
+    public void onSurfaceTextureUpdated(SurfaceTexture surface) {
+    }
 }
 
index 884812d..c4f698f 100644 (file)
@@ -1,7 +1,7 @@
 
 #pragma version(1)
 #pragma rs java_package_name(com.android.rs.livepreview)
-#pragma rs_fp_relaxed
+//#pragma rs_fp_relaxed
 
 static int gWidth;
 static int gHeight;