Renderscript gl context creation test

Change-Id: Ica3da4da7c7fbd4b5b1c2384289edcd4e94e8a1d
diff --git a/tests/AndroidManifest.xml b/tests/AndroidManifest.xml
index 172f6a2..b7f7cc6 100644
--- a/tests/AndroidManifest.xml
+++ b/tests/AndroidManifest.xml
@@ -1006,6 +1006,9 @@
                 <category android:name="android.intent.category.DEFAULT"/>
             </intent-filter>
         </activity>
+
+        <activity android:name="android.renderscript.cts.RenderscriptGLStubActivity"
+                  android:label="RenderscriptGLStub"/>
     </application>
 
     <!--Test for PackageManager, please put this at the very beginning-->
diff --git a/tests/src/android/renderscript/cts/RenderscriptGLStubActivity.java b/tests/src/android/renderscript/cts/RenderscriptGLStubActivity.java
new file mode 100644
index 0000000..3bb6212
--- /dev/null
+++ b/tests/src/android/renderscript/cts/RenderscriptGLStubActivity.java
@@ -0,0 +1,134 @@
+/*
+ * Copyright (C) 2011 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 android.renderscript.cts;
+
+import android.app.Activity;
+import android.os.Bundle;
+import android.content.Context;
+import android.content.res.Resources;
+import android.renderscript.*;
+
+import com.android.cts.stub.R;
+
+// Renderscript activity
+public class RenderscriptGLStubActivity extends Activity {
+    class StubActivityRS {
+        private Resources mRes;
+        private RenderScriptGL mRS;
+
+        private ScriptC mScript;
+
+        public StubActivityRS() {
+        }
+
+        // This provides us with the renderscript context and resources that
+        // allow us to create the script that does rendering
+        public void init(RenderScriptGL rs, Resources res) {
+            mRS = rs;
+            mRes = res;
+            initRS();
+        }
+
+        private void initRS() {
+            mScript = new ScriptC_stub_activity(mRS, mRes, R.raw.stub_activity);
+            mRS.bindRootScript(mScript);
+        }
+    }
+
+    class HelloWorldView extends RSSurfaceView {
+        // Renderscipt context
+        private RenderScriptGL mRS;
+        // Script that does the rendering
+        private StubActivityRS mRender;
+
+        public HelloWorldView(Context context) {
+            super(context);
+            ensureRenderScript();
+        }
+
+        private void ensureRenderScript() {
+            if (mRS == null) {
+                // Initialize renderscript with desired surface characteristics.
+                // In this case, just use the defaults
+                RenderScriptGL.SurfaceConfig sc = new RenderScriptGL.SurfaceConfig();
+                mRS = createRenderScriptGL(sc);
+                // Create an instance of the script that does the rendering
+                mRender = new StubActivityRS();
+                mRender.init(mRS, getResources());
+            }
+        }
+
+        @Override
+        protected void onAttachedToWindow() {
+            super.onAttachedToWindow();
+            ensureRenderScript();
+        }
+
+        @Override
+        protected void onDetachedFromWindow() {
+            // Handle the system event and clean up
+            mRender = null;
+            if (mRS != null) {
+                mRS = null;
+                destroyRenderScriptGL();
+            }
+        }
+    }
+
+    // Custom view to use with RenderScript
+    private HelloWorldView mView;
+
+    @Override
+    public void onCreate(Bundle icicle) {
+        super.onCreate(icicle);
+
+        // Create our view and set it as the content of our Activity
+        mView = new HelloWorldView(this);
+        setContentView(mView);
+    }
+
+    public void recreateView() {
+        mView = new HelloWorldView(this);
+        setContentView(mView);
+    }
+
+    public void recreateMultiView() {
+        HelloWorldView view1 = new HelloWorldView(this);
+        HelloWorldView view2 = new HelloWorldView(this);
+        setContentView(view1);
+        setContentView(view2);
+        mView = view2;
+    }
+
+    @Override
+    protected void onResume() {
+        // Ideally an app should implement onResume() and onPause()
+        // to take appropriate action when the activity loses focus
+        super.onResume();
+        mView.resume();
+    }
+
+    @Override
+    protected void onPause() {
+        // Ideally an app should implement onResume() and onPause()
+        // to take appropriate action when the activity loses focus
+        super.onPause();
+        mView.pause();
+    }
+
+}
+
diff --git a/tests/src/android/renderscript/cts/stub_activity.rs b/tests/src/android/renderscript/cts/stub_activity.rs
new file mode 100644
index 0000000..7e149f7
--- /dev/null
+++ b/tests/src/android/renderscript/cts/stub_activity.rs
@@ -0,0 +1,35 @@
+// Copyright (C) 2011 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)
+
+// Tell which java package name the reflected files should belong to
+#pragma rs java_package_name(android.renderscript.cts)
+
+// Built-in header with graphics API's
+#include "rs_graphics.rsh"
+
+int root(void) {
+
+    // Clear the background color
+    rsgClearColor(0.0f, 0.0f, 0.0f, 0.0f);
+    // Tell the runtime what the font color should be
+    rsgFontColor(1.0f, 1.0f, 1.0f, 1.0f);
+    // Introuduce ourselves to the world
+    rsgDrawText("Hello World!", 50, 50);
+
+    // Return value tells RS roughly how often to redraw
+    // in this case 20 ms
+    return 20;
+}
diff --git a/tests/tests/renderscript/src/android/renderscript/cts/ContextCreationTest.java b/tests/tests/renderscript/src/android/renderscript/cts/ContextCreationTest.java
new file mode 100644
index 0000000..84c8ade
--- /dev/null
+++ b/tests/tests/renderscript/src/android/renderscript/cts/ContextCreationTest.java
@@ -0,0 +1,96 @@
+/*
+ * Copyright (C) 2011 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 android.renderscript.cts;
+
+import dalvik.annotation.TestTargetClass;
+import android.renderscript.*;
+import android.test.ActivityInstrumentationTestCase2;
+import android.test.UiThreadTest;
+import android.util.Log;
+
+/**
+ * Tests for the RenderScriptGL class.
+ */
+@TestTargetClass(RenderScriptGL.class)
+public class ContextCreationTest extends
+        ActivityInstrumentationTestCase2<RenderscriptGLStubActivity> {
+
+    private static final int NUM_RECREATE_ITERATIONS_WITHOUT_DELAY = 50;
+
+    private static final int NUM_RECREATE_ITERATIONS_WITH_DELAY = 10;
+
+    private static final int RECREATE_DELAY = 100;
+
+    private static final boolean LOG_RECREATE = true;
+
+    private static final String TAG = "ContextCreationTest";
+
+    private RenderscriptGLStubActivity mActivity;
+
+    public ContextCreationTest() {
+        super(RenderscriptGLStubActivity.class);
+    }
+
+    @Override
+    protected void setUp() throws Exception {
+        super.setUp();
+        mActivity = getActivity();
+    }
+
+    /**
+     * Test repeated recreation of the renderscript context with a
+     * delay in between to detect hangs in the graphics drivers
+     */
+    @UiThreadTest
+    public void testCreationWithDelay() throws InterruptedException {
+        for (int i = 0; i < NUM_RECREATE_ITERATIONS_WITH_DELAY; i++) {
+            Thread.sleep(RECREATE_DELAY);
+            if (LOG_RECREATE) {
+                Log.w(TAG, "Recreate (w/ delay) step " + i + " - pause");
+            }
+            mActivity.recreateView();
+        }
+    }
+
+    /**
+     * Test repeated recreation of the renderscript context to
+     * detect hangs in the graphics drivers
+     */
+    @UiThreadTest
+    public void testCreationWithoutDelay() {
+        for (int i = 0; i < NUM_RECREATE_ITERATIONS_WITHOUT_DELAY; i++) {
+            if (LOG_RECREATE) {
+                Log.w(TAG, "Recreate (no delay) step " + i + " - pause");
+            }
+            mActivity.recreateView();
+        }
+    }
+
+    /**
+     * Test repeated recreation of the renderscript context with two
+     * created at a time to detect hangs in the graphics drivers
+     */
+    @UiThreadTest
+    public void testMultiviewCreationWithoutDelay() {
+        for (int i = 0; i < NUM_RECREATE_ITERATIONS_WITHOUT_DELAY; i++) {
+            if (LOG_RECREATE) {
+                Log.w(TAG, "Recreate multiview (no delay) step " + i + " - pause");
+            }
+            mActivity.recreateMultiView();
+        }
+    }
+}