am f1062114: am a3cc20ff: Merge "Initialize egl_cache with an app writeable file" into ics-mr1

* commit 'f1062114874f1cf46de479031d9ad3ad3cae1131':
  Initialize egl_cache with an app writeable file
diff --git a/core/java/android/app/ActivityThread.java b/core/java/android/app/ActivityThread.java
index a4714ca..f9896f7 100644
--- a/core/java/android/app/ActivityThread.java
+++ b/core/java/android/app/ActivityThread.java
@@ -45,6 +45,7 @@
 import android.net.IConnectivityManager;
 import android.net.Proxy;
 import android.net.ProxyProperties;
+import android.opengl.GLUtils;
 import android.os.AsyncTask;
 import android.os.Bundle;
 import android.os.Debug;
@@ -3714,6 +3715,24 @@
         }
     }
 
+    private void setupGraphicsSupport(LoadedApk info) {
+        try {
+            int uid = Process.myUid();
+            String[] packages = getPackageManager().getPackagesForUid(uid);
+
+            // If there are several packages in this application we won't
+            // initialize the graphics disk caches 
+            if (packages.length == 1) {
+                ContextImpl appContext = new ContextImpl();
+                appContext.init(info, null, this);
+
+                HardwareRenderer.setupDiskCache(appContext.getCacheDir());
+            }
+        } catch (RemoteException e) {
+            // Ignore
+        }
+    }    
+    
     private void handleBindApplication(AppBindData data) {
         mBoundApplication = data;
         mConfiguration = new Configuration(data.config);
@@ -3737,7 +3756,7 @@
                 HardwareRenderer.disable(false);
             }
         }
-
+        
         if (mProfiler.profileFd != null) {
             mProfiler.startProfiling();
         }
@@ -3773,6 +3792,8 @@
 
         data.info = getPackageInfoNoCheck(data.appInfo, data.compatInfo);
 
+        setupGraphicsSupport(data.info);        
+        
         /**
          * For system applications on userdebug/eng builds, log stack
          * traces of disk and network access to dropbox for analysis.
diff --git a/core/java/android/view/HardwareRenderer.java b/core/java/android/view/HardwareRenderer.java
index b86d21d..c2ac79d 100644
--- a/core/java/android/view/HardwareRenderer.java
+++ b/core/java/android/view/HardwareRenderer.java
@@ -34,6 +34,8 @@
 import javax.microedition.khronos.egl.EGLSurface;
 import javax.microedition.khronos.opengles.GL;
 
+import java.io.File;
+
 import static javax.microedition.khronos.egl.EGL10.*;
 
 /**
@@ -45,6 +47,11 @@
     static final String LOG_TAG = "HardwareRenderer";
 
     /**
+     * Name of the file that holds the shaders cache.
+     */
+    private static final String CACHE_PATH_SHADERS = "com.android.opengl.shaders_cache";
+
+    /**
      * Turn on to only refresh the parts of the screen that need updating.
      * When turned on the property defined by {@link #RENDER_DIRTY_REGIONS_PROPERTY}
      * must also have the value "true". 
@@ -200,6 +207,18 @@
     abstract int getHeight();
 
     /**
+     * Sets the directory to use as a persistent storage for hardware rendering
+     * resources.
+     * 
+     * @param cacheDir A directory the current process can write to
+     */
+    public static void setupDiskCache(File cacheDir) {
+        nSetupShadersDiskCache(new File(cacheDir, CACHE_PATH_SHADERS).getAbsolutePath());
+    }
+
+    private static native void nSetupShadersDiskCache(String cacheFile);
+
+    /**
      * Interface used to receive callbacks whenever a view is drawn by
      * a hardware renderer instance.
      */
diff --git a/core/jni/Android.mk b/core/jni/Android.mk
index 59a03e7..71c5d26 100644
--- a/core/jni/Android.mk
+++ b/core/jni/Android.mk
@@ -53,6 +53,7 @@
 	android_view_InputQueue.cpp \
 	android_view_KeyEvent.cpp \
 	android_view_KeyCharacterMap.cpp \
+	android_view_HardwareRenderer.cpp \
 	android_view_GLES20Canvas.cpp \
 	android_view_MotionEvent.cpp \
 	android_view_PointerIcon.cpp \
@@ -160,6 +161,7 @@
 	$(JNI_H_INCLUDE) \
 	$(LOCAL_PATH)/android/graphics \
 	$(LOCAL_PATH)/../../libs/hwui \
+	$(LOCAL_PATH)/../../opengl/libs \
 	$(call include-path-for, bluedroid) \
 	$(call include-path-for, libhardware)/hardware \
 	$(call include-path-for, libhardware_legacy)/hardware_legacy \
diff --git a/core/jni/AndroidRuntime.cpp b/core/jni/AndroidRuntime.cpp
index 654d66d..6d1410c 100644
--- a/core/jni/AndroidRuntime.cpp
+++ b/core/jni/AndroidRuntime.cpp
@@ -116,6 +116,7 @@
 extern int register_android_graphics_PixelFormat(JNIEnv* env);
 extern int register_android_view_Display(JNIEnv* env);
 extern int register_android_view_GLES20Canvas(JNIEnv* env);
+extern int register_android_view_HardwareRenderer(JNIEnv* env);
 extern int register_android_view_Surface(JNIEnv* env);
 extern int register_android_view_TextureView(JNIEnv* env);
 extern int register_android_database_CursorWindow(JNIEnv* env);
@@ -1101,6 +1102,7 @@
     REG_JNI(register_android_graphics_PixelFormat),
     REG_JNI(register_android_graphics_Graphics),
     REG_JNI(register_android_view_GLES20Canvas),
+    REG_JNI(register_android_view_HardwareRenderer),
     REG_JNI(register_android_view_Surface),
     REG_JNI(register_android_view_TextureView),
     REG_JNI(register_com_google_android_gles_jni_EGLImpl),
diff --git a/core/jni/android_view_HardwareRenderer.cpp b/core/jni/android_view_HardwareRenderer.cpp
new file mode 100644
index 0000000..09809ec
--- /dev/null
+++ b/core/jni/android_view_HardwareRenderer.cpp
@@ -0,0 +1,54 @@
+/*
+ * Copyright (C) 2010 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.
+ */
+
+#define LOG_TAG "HardwareRenderer"
+
+#include "jni.h"
+#include <nativehelper/JNIHelp.h>
+#include <android_runtime/AndroidRuntime.h>
+
+#include <EGL/egl_cache.h>
+
+namespace android {
+
+// ----------------------------------------------------------------------------
+// Misc
+// ----------------------------------------------------------------------------
+
+static void android_view_HardwareRenderer_setupShadersDiskCache(JNIEnv* env, jobject clazz,
+        jstring diskCachePath) {
+
+    const char* cacheArray = env->GetStringUTFChars(diskCachePath, NULL);
+    egl_cache_t::get()->setCacheFilename(cacheArray);
+    env->ReleaseStringUTFChars(diskCachePath, cacheArray);
+}
+
+// ----------------------------------------------------------------------------
+// JNI Glue
+// ----------------------------------------------------------------------------
+
+const char* const kClassPathName = "android/view/HardwareRenderer";
+
+static JNINativeMethod gMethods[] = {
+    { "nSetupShadersDiskCache", "(Ljava/lang/String;)V",
+            (void*) android_view_HardwareRenderer_setupShadersDiskCache },
+};
+
+int register_android_view_HardwareRenderer(JNIEnv* env) {
+    return AndroidRuntime::registerNativeMethods(env, kClassPathName, gMethods, NELEM(gMethods));
+}
+
+};