Rename ioReceive and ioSend

First checkin of Allocation IO test

Change-Id: I26379e442796caab95a089dbb42b02192f4cc563
This commit is contained in:
Jason Sams
2012-03-29 17:58:15 -07:00
parent 75ec67848c
commit c5f519c5ec
9 changed files with 566 additions and 2 deletions

View File

@@ -365,7 +365,7 @@ public class Allocation extends BaseObj {
* @hide
*
*/
public void ioSendOutput() {
public void ioSend() {
if ((mUsage & USAGE_IO_OUTPUT) == 0) {
throw new RSIllegalArgumentException(
"Can only send buffer if IO_OUTPUT usage specified.");
@@ -374,13 +374,21 @@ public class Allocation extends BaseObj {
mRS.nAllocationIoSend(getID());
}
/**
* Delete once code is updated.
* @hide
*/
public void ioSendOutput() {
ioSend();
}
/**
* Receive the latest input into the Allocation.
*
* @hide
*
*/
public void ioGetInput() {
public void ioReceive() {
if ((mUsage & USAGE_IO_INPUT) == 0) {
throw new RSIllegalArgumentException(
"Can only receive if IO_INPUT usage specified.");

View File

@@ -0,0 +1,29 @@
#
# Copyright (C) 2012 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.
# You may obtain a copy of the License at
#
# http://www.apache.org/licenses/LICENSE-2.0
#
# Unless required by applicable law or agreed to in writing, software
# distributed under the License is distributed on an "AS IS" BASIS,
# WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
# See the License for the specific language governing permissions and
# limitations under the License.
#
LOCAL_PATH := $(call my-dir)
include $(CLEAR_VARS)
LOCAL_MODULE_TAGS := optional
LOCAL_SRC_FILES := $(call all-java-files-under, src) $(call all-renderscript-files-under, src)
# TODO: build fails with this set
# LOCAL_SDK_VERSION := current
LOCAL_PACKAGE_NAME := RsSurfaceTextureOpaque
include $(BUILD_PACKAGE)

View File

@@ -0,0 +1,21 @@
<?xml version="1.0" encoding="utf-8"?>
<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.example.android.rs.sto">
<uses-sdk android:minSdkVersion="14" />
<uses-permission android:name="android.permission.CAMERA" />
<uses-feature android:name="android.hardware.camera" />
<uses-feature android:name="android.hardware.camera.autofocus" />
<application
android:label="RsSurfaceTextureOpaque"
android:hardwareAccelerated="true"
android:icon="@drawable/test_pattern">
<activity android:name="SurfaceTextureOpaque">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
</application>
</manifest>

Binary file not shown.

After

Width:  |  Height:  |  Size: 307 B

View File

@@ -0,0 +1,234 @@
/*
* Copyright (C) 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.sto;
import android.graphics.SurfaceTexture;
import android.hardware.Camera;
import android.os.SystemClock;
import android.util.Log;
import java.io.IOException;
import java.util.List;
public class CameraCapture {
public interface CameraFrameListener {
public void onNewCameraFrame();
}
static final int FRAMES_PER_SEC = 30;
private Camera mCamera;
private SurfaceTexture mSurfaceTexture;
private int mProgram;
private int mCameraTransformHandle;
private int mTexSamplerHandle;
private int mTexCoordHandle;
private int mPosCoordHandle;
private float[] mCameraTransform = new float[16];
private int mCameraId = 0;
private int mWidth;
private int mHeight;
private long mStartCaptureTime = 0;
private boolean mNewFrameAvailable = false;
private boolean mIsOpen = false;
private CameraFrameListener mListener;
public synchronized void beginCapture(int cameraId, int width, int height,
SurfaceTexture st) {
mCameraId = cameraId;
mSurfaceTexture = st;
// Open the camera
openCamera(width, height);
// Start the camera
mStartCaptureTime = SystemClock.elapsedRealtime();
mCamera.startPreview();
mIsOpen = true;
}
public void getCurrentFrame() {
if (checkNewFrame()) {
if (mStartCaptureTime > 0 && SystemClock.elapsedRealtime() - mStartCaptureTime > 2000) {
// Lock white-balance and exposure for effects
Log.i("CC", "Locking white-balance and exposure!");
Camera.Parameters params = mCamera.getParameters();
params.setAutoWhiteBalanceLock(true);
params.setAutoExposureLock(true);
//mCamera.setParameters(params);
mStartCaptureTime = 0;
}
mSurfaceTexture.updateTexImage();
mSurfaceTexture.getTransformMatrix(mCameraTransform);
// display it here
}
}
public synchronized boolean hasNewFrame() {
return mNewFrameAvailable;
}
public synchronized void endCapture() {
mIsOpen = false;
if (mCamera != null) {
mCamera.release();
mCamera = null;
mSurfaceTexture = null;
}
}
public synchronized boolean isOpen() {
return mIsOpen;
}
public int getWidth() {
return mWidth;
}
public int getHeight() {
return mHeight;
}
public void setCameraFrameListener(CameraFrameListener listener) {
mListener = listener;
}
private void openCamera(int width, int height) {
// Setup camera
mCamera = Camera.open(mCameraId);
mCamera.setParameters(calcCameraParameters(width, height));
// Create camera surface texture
try {
mCamera.setPreviewTexture(mSurfaceTexture);
} catch (IOException e) {
throw new RuntimeException("Could not bind camera surface texture: " +
e.getMessage() + "!");
}
// Connect SurfaceTexture to callback
mSurfaceTexture.setOnFrameAvailableListener(onCameraFrameAvailableListener);
}
private Camera.Parameters calcCameraParameters(int width, int height) {
Camera.Parameters params = mCamera.getParameters();
params.setPreviewSize(mWidth, mHeight);
// Find closest size
int closestSize[] = findClosestSize(width, height, params);
mWidth = closestSize[0];
mHeight = closestSize[1];
params.setPreviewSize(mWidth, mHeight);
// Find closest FPS
int closestRange[] = findClosestFpsRange(FRAMES_PER_SEC, params);
params.setPreviewFpsRange(closestRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX],
closestRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
return params;
}
private int[] findClosestSize(int width, int height, Camera.Parameters parameters) {
List<Camera.Size> previewSizes = parameters.getSupportedPreviewSizes();
int closestWidth = -1;
int closestHeight = -1;
int smallestWidth = previewSizes.get(0).width;
int smallestHeight = previewSizes.get(0).height;
for (Camera.Size size : previewSizes) {
// Best match defined as not being larger in either dimension than
// the requested size, but as close as possible. The below isn't a
// stable selection (reording the size list can give different
// results), but since this is a fallback nicety, that's acceptable.
if ( size.width <= width &&
size.height <= height &&
size.width >= closestWidth &&
size.height >= closestHeight) {
closestWidth = size.width;
closestHeight = size.height;
}
if ( size.width < smallestWidth &&
size.height < smallestHeight) {
smallestWidth = size.width;
smallestHeight = size.height;
}
}
if (closestWidth == -1) {
// Requested size is smaller than any listed size; match with smallest possible
closestWidth = smallestWidth;
closestHeight = smallestHeight;
}
int[] closestSize = {closestWidth, closestHeight};
return closestSize;
}
private int[] findClosestFpsRange(int fps, Camera.Parameters params) {
List<int[]> supportedFpsRanges = params.getSupportedPreviewFpsRange();
int[] closestRange = supportedFpsRanges.get(0);
int fpsk = fps * 1000;
int minDiff = 1000000;
for (int[] range : supportedFpsRanges) {
int low = range[Camera.Parameters.PREVIEW_FPS_MIN_INDEX];
int high = range[Camera.Parameters.PREVIEW_FPS_MAX_INDEX];
if (low <= fpsk && high >= fpsk) {
int diff = (fpsk - low) + (high - fpsk);
if (diff < minDiff) {
closestRange = range;
minDiff = diff;
}
}
}
Log.i("CC", "Found closest range: "
+ closestRange[Camera.Parameters.PREVIEW_FPS_MIN_INDEX] + " - "
+ closestRange[Camera.Parameters.PREVIEW_FPS_MAX_INDEX]);
return closestRange;
}
private synchronized void signalNewFrame() {
mNewFrameAvailable = true;
if (mListener != null) {
mListener.onNewCameraFrame();
}
}
private synchronized boolean checkNewFrame() {
if (mNewFrameAvailable) {
mNewFrameAvailable = false;
return true;
}
return false;
}
private SurfaceTexture.OnFrameAvailableListener onCameraFrameAvailableListener =
new SurfaceTexture.OnFrameAvailableListener() {
@Override
public void onFrameAvailable(SurfaceTexture surfaceTexture) {
signalNewFrame();
}
};
}

View File

@@ -0,0 +1,71 @@
/*
* Copyright (C) 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.sto;
import android.app.Activity;
import android.os.Bundle;
import android.provider.Settings.System;
import android.util.Log;
import android.view.View;
import android.graphics.SurfaceTexture;
import java.lang.Runtime;
public class SurfaceTextureOpaque extends Activity {
private SurfaceTextureOpaqueView mView;
CameraCapture mCC;
@Override
public void onCreate(Bundle icicle) {
super.onCreate(icicle);
mView = new SurfaceTextureOpaqueView(this);
setContentView(mView);
}
@Override
protected void onResume() {
super.onResume();
mView.resume();
startCamera();
}
@Override
protected void onPause() {
super.onPause();
mView.pause();
mCC.endCapture();
}
cfl mCFL;
public void startCamera() {
mCC = new CameraCapture();
mCFL = new cfl();
mCC.setCameraFrameListener(mCFL);
mCC.beginCapture(1, 640, 480, mView.getST());
}
public class cfl implements CameraCapture.CameraFrameListener {
public void onNewCameraFrame() {
mView.mRender.newFrame();
}
}
}

View File

@@ -0,0 +1,83 @@
/*
* Copyright (C) 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.sto;
import android.content.res.Resources;
import android.renderscript.*;
import android.graphics.SurfaceTexture;
import android.util.Log;
public class SurfaceTextureOpaqueRS {
static final private int NUM_CAMERA_PREVIEW_BUFFERS = 2;
public SurfaceTextureOpaqueRS() {
}
private Resources mRes;
private RenderScriptGL mRS;
private ScriptC_sto mScript;
private SurfaceTexture mST;
private Allocation mSto;
private Allocation mSto2;
private Allocation mRto;
private ProgramFragment mPF;
public void init(RenderScriptGL rs, Resources res) {
mRS = rs;
mRes = res;
Type.Builder tb = new Type.Builder(mRS, Element.RGBA_8888(mRS));
tb.setX(640);
tb.setY(480);
mSto = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_TEXTURE |
Allocation.USAGE_IO_INPUT);
mRto = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_RENDER_TARGET |
Allocation.USAGE_IO_OUTPUT);
mSto2 = Allocation.createTyped(mRS, tb.create(), Allocation.USAGE_GRAPHICS_TEXTURE |
Allocation.USAGE_IO_INPUT);
mST = mSto.getSurfaceTexture();
mRto.setSurfaceTexture(mSto2.getSurfaceTexture());
ProgramFragmentFixedFunction.Builder pfb = new ProgramFragmentFixedFunction.Builder(rs);
pfb.setTexture(ProgramFragmentFixedFunction.Builder.EnvMode.REPLACE,
ProgramFragmentFixedFunction.Builder.Format.RGBA, 0);
mPF = pfb.create();
mPF.bindSampler(Sampler.CLAMP_NEAREST(mRS), 0);
rs.bindProgramFragment(mPF);
mScript = new ScriptC_sto(mRS, mRes, R.raw.sto);
mScript.set_sto(mSto);
mScript.set_rto(mRto);
mScript.set_sto2(mSto2);
mScript.set_pf(mPF);
mRS.bindRootScript(mScript);
android.util.Log.v("sto", "Init complete");
}
SurfaceTexture getST() {
return mST;
}
public void newFrame() {
mSto.ioReceive();
}
}

View File

@@ -0,0 +1,60 @@
/*
* Copyright (C) 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package com.example.android.rs.sto;
import android.renderscript.RSSurfaceView;
import android.renderscript.RenderScriptGL;
import android.content.Context;
import android.content.res.Resources;
import android.graphics.SurfaceTexture;
import android.util.Log;
public class SurfaceTextureOpaqueView extends RSSurfaceView {
public SurfaceTextureOpaqueView(Context context) {
super(context);
}
RenderScriptGL mRS;
SurfaceTextureOpaqueRS mRender;
@Override
protected void onAttachedToWindow() {
super.onAttachedToWindow();
}
@Override
protected void onDetachedFromWindow() {
super.onDetachedFromWindow();
if (mRS != null) {
mRS = null;
destroyRenderScriptGL();
}
}
SurfaceTexture getST() {
RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
mRS = createRenderScriptGL(sc);
mRender = new SurfaceTextureOpaqueRS();
mRender.init(mRS, getResources());
return mRender.getST();
}
}

View File

@@ -0,0 +1,58 @@
/*
* Copyright (C) 2012 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.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#pragma version(1)
#pragma rs java_package_name(com.example.android.rs.sto)
#pragma stateFragment(parent)
#include "rs_graphics.rsh"
rs_program_fragment pf;
rs_allocation sto; // camera in
rs_allocation sto2;
rs_allocation rto; // render target
int root() {
rsgBindTexture(pf, 0, sto);
#if 1
rsgBindColorTarget(rto, 0);
rsgClearColor(0.f, 1.f, 0.f, 1.f);
rsgDrawQuadTexCoords(0, 0, 0, 0,0,
0,500,0, 1,0,
500,500,0, 1, 1,
500, 0, 0, 0, 1 );
rsgClearColorTarget(0);
// io ops
rsAllocationIoSend(rto);
rsAllocationIoReceive(sto2);
rsgBindTexture(pf, 0, sto2);
#endif
rsgClearColor(0.f, 1.f, 0.f, 1.f);
rsgDrawQuadTexCoords(0, 0, 0, 0,0,
0,500,0, 1,0,
500,500,0, 1, 1,
500, 0, 0, 0, 1 );
return 1;
}