2013-11-26 65 views
4

我对RenderScript完全陌生。我试图在http://www.youtube.com/watch?v=5jz0kSuR2j4工作中得到Romain Guy显示的一个简单示例。RenderScript代码不能在没有rsDebug的情况下工作

首先,这是代码。

MainActivity.java

package com.example.rsgray; 

    import android.app.Activity; 
    import android.graphics.Bitmap; 
    import android.graphics.BitmapFactory; 
    import android.os.Bundle; 
    import android.renderscript.Allocation; 
    import android.renderscript.RenderScript; 
    import android.view.Window; 
    import android.view.WindowManager; 
    import android.widget.ImageView; 
    import android.widget.ImageView.ScaleType; 

    public class MainActivity extends Activity { 

@Override 
protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 

    getWindow().addFlags(WindowManager.LayoutParams.FLAG_FULLSCREEN); 
    requestWindowFeature(Window.FEATURE_NO_TITLE); 
    ImageView iv = new ImageView(this); 
    iv.setScaleType(ScaleType.CENTER_CROP); 
    setContentView(iv); 

    drawGrayScale(iv, R.drawable.tulips); 
} 

private void drawGrayScale(ImageView iv, int image) { 
    Bitmap in = BitmapFactory.decodeResource(getResources(), image); 
    Bitmap out = Bitmap.createBitmap(in.getWidth(), in.getHeight(), in.getConfig()); 

    RenderScript rs = RenderScript.create(MainActivity.this); 
    Allocation inAlloc = Allocation.createFromBitmap(rs, in); 
    Allocation outAlloc = Allocation.createTyped(rs, inAlloc.getType()); 

    ScriptC_gray script = new ScriptC_gray(rs, getResources(), R.raw.gray); 
    script.set_in(inAlloc); 
    script.set_out(outAlloc); 
    script.set_script(script); 

    script.invoke_filter(); 

    outAlloc.copyTo(out); 
    iv.setImageBitmap(out); 
} 

}

gray.rs

#pragma version(1) 
    #pragma rs java_package_name(com.example.rsgray) 
    #pragma rs_fp_relaxed 

    rs_allocation in; 
    rs_allocation out; 
    rs_script script; 

    const static float3 gray = { 0.3f, 0.6f, 0.1f }; 

    void root(const uchar4* v_in, uchar4* v_out, const void* usrData, uint32_t x, uint32_t y) { 
     rsDebug("gray.rs", rsUptimeMillis()); 
     float4 inPixel = rsUnpackColor8888(*v_in); 
     float3 result = dot(inPixel.rgb, gray); 
     *v_out = rsPackColorTo8888(result); 
    } 


    void filter() { 
     rsForEach(script, in, out); 
    } 

,最后的AndroidManifest.xml

<?xml version="1.0" encoding="utf-8"?> 
<manifest xmlns:android="http://schemas.android.com/apk/res/android" 
package="com.example.rsgray" 
android:versionCode="1" 
android:versionName="1.0" > 

<uses-sdk 
    android:minSdkVersion="16" 
    android:targetSdkVersion="16" /> 

<application 
    android:allowBackup="true" 
    android:icon="@drawable/ic_launcher" 
    android:label="@string/app_name" 
    android:theme="@style/AppTheme" > 
    <activity 
     android:name="com.example.rsgray.MainActivity" 
     android:label="@string/app_name" > 
     <intent-filter> 
      <action android:name="android.intent.action.MAIN" /> 

      <category android:name="android.intent.category.LAUNCHER" /> 
     </intent-filter> 
    </activity> 
</application> 

</manifest> 

RES /抽拉-nodpi包含郁金香。 JPG。你不会在Romain Guy的代码中找到我在gray.rs中添加的rsDebug()语句。

无论如何,代码编译,有或没有rsDebug()。但是,当我尝试在设备上运行应用程序时,问题就开始了。

我试图在运行Android 4.4的Nexus 4上运行该应用程序。使用所示的代码,它工作正常,我得到一个灰度图像。但是,如果我注释掉rsDebug()行,该应用程序不起作用。我只是得到一个黑屏。

这里是logcat输出。

  1. 随着rsDebug():

    11-26 16:22:41.563: D/dalvikvm(21523): GC_FOR_ALLOC freed 69K, 2% free 9046K/9148K, paused 16ms, total 16ms 
    11-26 16:22:41.573: I/dalvikvm-heap(21523): Grow heap (frag case) to 11.862MB for 3145744-byte allocation 
    11-26 16:22:41.603: D/dalvikvm(21523): GC_FOR_ALLOC freed <1K, 1% free 12118K/12224K, paused 30ms, total 30ms 
    11-26 16:22:41.683: D/dalvikvm(21523): GC_FOR_ALLOC freed <1K, 1% free 12117K/12224K, paused 11ms, total 11ms 
    11-26 16:22:41.693: I/dalvikvm-heap(21523): Grow heap (frag case) to 14.862MB for 3145744-byte allocation 
    11-26 16:22:41.703: D/dalvikvm(21523): GC_FOR_ALLOC freed 0K, 1% free 15189K/15300K, paused 14ms, total 14ms 
    11-26 16:22:41.753: V/RenderScript(21523): 0x74ed5020 Launching thread(s), CPUs 4 
    11-26 16:22:41.753: W/Adreno-RS(21523): <rsdVendorAllocationDestroyQCOM:194>: rsdVendorAllocationDestroy: No context! 
    11-26 16:22:41.753: E/RenderScript(21523): Successfully loaded runtime: libRSDriver_adreno.so 
    11-26 16:22:41.783: D/dalvikvm(21523): GC_FOR_ALLOC freed <1K, 1% free 15201K/15300K, paused 11ms, total 11ms 
    11-26 16:22:41.803: D/dalvikvm(21523): GC_FOR_ALLOC freed <1K, 1% free 15202K/15300K, paused 11ms, total 12ms 
    11-26 16:22:41.813: W/Adreno-RS(21523): <rsdCompileBitcode:289>: Header check (bitcode_SHA1) failed for /data/data/com.example.rsgray/cache/com.android.renderscript.cache/gray-adreno.meta 
    11-26 16:22:41.813: W/Adreno-RS(21523): <rsdDumpCompileLog:499>: Build log for gray: RS code uses long(i64) data type 
    11-26 16:22:41.813: W/Adreno-RS(21523): RS bitcode does not contain an invokable compute root 
    11-26 16:22:41.813: W/Adreno-RS(21523): <rsdVendorScriptInitQCOM:610>: ERROR: __BuildProgram returned -11 
    11-26 16:22:41.813: D/RenderScript(21523): long gray.rs 18142906 0x114d6ba 
    11-26 16:22:41.813: D/RenderScript(21523): long gray.rs 18142906 0x114d6ba 
    11-26 16:22:41.813: D/RenderScript(21523): long gray.rs 18142906 0x114d6ba 
    11-26 16:22:41.813: D/RenderScript(21523): long gray.rs 18142906 0x114d6ba 
    . 
    . a million more such lines 
    . 
    11-26 16:22:44.686: D/RenderScript(21523): long gray.rs 18145771 0x114e1eb 
    11-26 16:22:44.686: D/RenderScript(21523): long gray.rs 18145771 0x114e1eb 
    11-26 16:22:44.686: D/RenderScript(21523): long gray.rs 18145771 0x114e1eb 
    11-26 16:22:44.686: D/RenderScript(21523): long gray.rs 18145771 0x114e1eb 
    11-26 16:22:44.717: I/Adreno-EGL(21523): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM build: (CL3776187) 
    11-26 16:22:44.717: I/Adreno-EGL(21523): OpenGL ES Shader Compiler Version: 
    11-26 16:22:44.717: I/Adreno-EGL(21523): Build Date: 10/15/13 Tue 
    11-26 16:22:44.717: I/Adreno-EGL(21523): Local Branch: 
    11-26 16:22:44.717: I/Adreno-EGL(21523): Remote Branch: partner/upstream 
    11-26 16:22:44.717: I/Adreno-EGL(21523): Local Patches: 
    11-26 16:22:44.717: I/Adreno-EGL(21523): Reconstruct Branch: 
    11-26 16:22:44.747: D/OpenGLRenderer(21523): Enabling debug mode 0 
    
  2. 没有rsDebug():

    11-26 16:23:41.327: D/dalvikvm(21902): GC_FOR_ALLOC freed 56K, 1% free 9046K/9136K, paused 18ms, total 18ms 
    11-26 16:23:41.337: I/dalvikvm-heap(21902): Grow heap (frag case) to 11.862MB for 3145744-byte allocation 
    11-26 16:23:41.367: D/dalvikvm(21902): GC_FOR_ALLOC freed <1K, 1% free 12118K/12212K, paused 27ms, total 27ms 
    11-26 16:23:41.427: D/dalvikvm(21902): GC_FOR_ALLOC freed <1K, 1% free 12117K/12212K, paused 11ms, total 11ms 
    11-26 16:23:41.437: I/dalvikvm-heap(21902): Grow heap (frag case) to 14.862MB for 3145744-byte allocation 
    11-26 16:23:41.447: D/dalvikvm(21902): GC_FOR_ALLOC freed 0K, 1% free 15189K/15288K, paused 12ms, total 12ms 
    11-26 16:23:41.477: V/RenderScript(21902): 0x74ed4f18 Launching thread(s), CPUs 4 
    11-26 16:23:41.477: W/Adreno-RS(21902): <rsdVendorAllocationDestroyQCOM:194>: rsdVendorAllocationDestroy: No context! 
    11-26 16:23:41.477: E/RenderScript(21902): Successfully loaded runtime: libRSDriver_adreno.so 
    11-26 16:23:41.507: D/dalvikvm(21902): GC_FOR_ALLOC freed <1K, 1% free 15201K/15288K, paused 12ms, total 13ms 
    11-26 16:23:41.527: D/dalvikvm(21902): GC_FOR_ALLOC freed <1K, 1% free 15202K/15288K, paused 12ms, total 12ms 
    11-26 16:23:41.537: D/bcc(21902): Cache /data/data/com.example.rsgray/cache/com.android.renderscript.cache/gray.o.info is dirty due to the source it dependends on has been changed: 
    11-26 16:23:43.419: I/Adreno-EGL(21902): <qeglDrvAPI_eglInitialize:320>: EGL 1.4 QUALCOMM build: (CL3776187) 
    11-26 16:23:43.419: I/Adreno-EGL(21902): OpenGL ES Shader Compiler Version: 
    11-26 16:23:43.419: I/Adreno-EGL(21902): Build Date: 10/15/13 Tue 
    11-26 16:23:43.419: I/Adreno-EGL(21902): Local Branch: 
    11-26 16:23:43.419: I/Adreno-EGL(21902): Remote Branch: partner/upstream 
    11-26 16:23:43.419: I/Adreno-EGL(21902): Local Patches: 
    11-26 16:23:43.419: I/Adreno-EGL(21902): Reconstruct Branch: 
    11-26 16:23:43.449: D/OpenGLRenderer(21902): Enabling debug mode 0 
    

我无法理解调试语句是如何影响的方式,其中,应用运行。任何帮助,为什么我面临这个问题将不胜感激。

所以这是我的问题的第一部分。

第二部分:当我尝试在运行Android 4.1.2的Galaxy Note 10.1 N8013上运行完全相同的代码并且在运行Android 4.1.2的APQ8064的另一块硬件上运行时,该应用程序无法运行。

这里的logcat的输出与我的硬件:

01-02 00:00:51.043: E/Trace(1684): error opening trace file: No such file or directory (2) 
    01-02 00:00:51.043: D/ActivityThread(1684): setTargetHeapUtilization:0.25 
    01-02 00:00:51.043: D/ActivityThread(1684): setTargetHeapIdealFree:8388608 
    01-02 00:00:51.053: D/ActivityThread(1684): setTargetHeapConcurrentStart:2097152 
    01-02 00:00:51.183: V/RenderScript(1684): rsContextCreate dev=0x7276fdd0 
    01-02 00:00:51.183: V/RenderScript(1684): 0x616350e0 Launching thread(s), CPUs 4 
    01-02 00:00:51.203: V/ScriptC(1684): Create script for resource = gray 
    01-02 00:00:51.203: E/bcc(1684): CPU is krait2 
    01-02 00:00:51.274: W/bcc(1684): Unable to open /data/data/com.example.rsgray/cache/com.android.renderscript.cache/gray.o in read mode. (reason: No such file or directory) 
    01-02 00:00:51.404: A/libc(1684): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 1701 (.example.rsgray) 

最后,这里是一个与三星平板的logcat的输出:

01-02 10:34:47.470: D/dalvikvm(19022): GC_FOR_ALLOC freed 48K, 11% free 7046K/7875K, paused 13ms, total 13ms 
    01-02 10:34:47.475: I/dalvikvm-heap(19022): Grow heap (frag case) to 10.495MB for 3145744-byte allocation 
    01-02 10:34:47.495: D/dalvikvm(19022): GC_CONCURRENT freed <1K, 9% free 10117K/11015K, paused 11ms+1ms, total 18ms 
    01-02 10:34:47.585: D/dalvikvm(19022): GC_FOR_ALLOC freed <1K, 9% free 10117K/11015K, paused 10ms, total 10ms 
    01-02 10:34:47.590: I/dalvikvm-heap(19022): Grow heap (frag case) to 13.494MB for 3145744-byte allocation 
    01-02 10:34:47.610: D/dalvikvm(19022): GC_CONCURRENT freed <1K, 7% free 13189K/14151K, paused 11ms+3ms, total 23ms 
    01-02 10:34:47.610: D/dalvikvm(19022): WAIT_FOR_CONCURRENT_GC blocked 12ms 
    01-02 10:34:47.610: V/RenderScript(19022): rsContextCreate dev=0x40e3b288 
    01-02 10:34:47.615: V/RenderScript(19022): 0x411e3008 Launching thread(s), CPUs 3 
    01-02 10:34:47.625: V/ScriptC(19022): Create script for resource = gray 
    01-02 10:34:47.625: I/bcc(19022): LIBBCC build time: 2013/01/09 23:19:52 
    01-02 10:34:47.625: I/bcc(19022): LIBBCC build revision: Unknown (not git) 
    01-02 10:34:47.645: D/StopWatch(19022): StopWatch calcFileSHA1 time (us): 17719 
    01-02 10:34:47.650: D/StopWatch(19022): StopWatch calcFileSHA1 time (us): 3031 
    01-02 10:34:47.650: W/bcc(19022): Unable to open file in read mode. (reason: No such file or directory) 
    01-02 10:34:47.750: A/libc(19022): Fatal signal 11 (SIGSEGV) at 0xdeadbaad (code=1), thread 19053 (.example.rsgray) 

这就是它。如果任何人有任何想法如何解释这一点,请让我知道。

谢谢!

+0

你可以尝试没有rsDebug,但与强制CPU的setprop? “adb shell setprop debug.rs.default-CPU-driver 1” –

+0

感谢您的回复Jason。这使得我的Nexus 4上的应用程序工作。没有运气在其他设备上。 但是,我的答案有问题。我开始使用RenderScript的主要原因是希望能够在GPU上完成一些计算。我知道无法强制RenderScript在GPU上运行。但是不会强制我的应用程序只能在CPU上运行? – Akshay

+0

这不是一个答案,我只是想缩小发生了什么。这看起来像一个bug,但我无法从这里的位复制它。如果您可以在https://code.google.com/p/android/issues/list上提交apk错误,我会进一步了解它。 –

回答

0

我刚刚遇到同样的问题。当rsDebug处于同一个函数的某个位置时,它似乎被修复了。这对我来说是固定的:

if(x > 0) { 
    if(x < 0) { 
     rsDebug("Workaround for renderscript bug", 0.0f); 
    } 

    /* Code */ 
} 

使用这个,rsDebug从来没有实际调用,所以它不会输出任何东西。

注意这不起作用:

if(0) { 
    rsDebug("Workaround for renderscript bug", 0.0f); 
} 

这可能是最优化的。

相关问题