diff mbox

[Branch,~glmark2-dev/glmark2/trunk] Rev 154: Android, Model: Fix model listing and discovery on Android.

Message ID 20111013140520.8587.15248.launchpad@ackee.canonical.com
State Accepted
Headers show

Commit Message

alexandros.frantzis@linaro.org Oct. 13, 2011, 2:05 p.m. UTC
Merge authors:
  Alexandros Frantzis (afrantzis)
------------------------------------------------------------
revno: 154 [merge]
committer: Alexandros Frantzis <alexandros.frantzis@linaro.org>
branch nick: trunk
timestamp: Thu 2011-10-13 17:00:10 +0300
message:
  Android,Model: Fix model listing and discovery on Android.
modified:
  src/android.cpp
  src/model.cpp
  src/util.cpp
  src/util.h


--
lp:glmark2
https://code.launchpad.net/~glmark2-dev/glmark2/trunk

You are subscribed to branch lp:glmark2.
To unsubscribe from this branch go to https://code.launchpad.net/~glmark2-dev/glmark2/trunk/+edit-subscription
diff mbox

Patch

=== modified file 'src/android.cpp'
--- src/android.cpp	2011-10-10 16:10:04 +0000
+++ src/android.cpp	2011-10-13 13:44:52 +0000
@@ -73,6 +73,8 @@ 
 Java_org_linaro_glmark2_Glmark2Renderer_nativeInit(JNIEnv* env, jclass clazz,
                                                    jobject asset_manager)
 {
+    Util::android_set_asset_manager(AAssetManager_fromJava(env, asset_manager));
+
     g_canvas = new CanvasAndroid(100, 100);
     g_canvas->init();
 
@@ -93,8 +95,6 @@ 
     Benchmark::register_scene(*new SceneBuffer(*g_canvas));
 
     add_default_benchmarks(g_benchmarks);
-
-    Util::android_set_asset_manager(AAssetManager_fromJava(env, asset_manager));
 }
 
 void

=== modified file 'src/model.cpp'
--- src/model.cpp	2011-10-07 18:19:11 +0000
+++ src/model.cpp	2011-10-13 13:56:26 +0000
@@ -27,7 +27,6 @@ 
 #include "options.h"
 #include "util.h"
 #include "float.h"
-#include <dirent.h>
 #include <fstream>
 #include <sstream>
 #include <memory>
@@ -603,34 +602,7 @@ 
 
 namespace ModelPrivate
 {
-
-void
-list_files(const string& dirName, vector<string>& fileVec)
-{
-    DIR* dir = opendir(dirName.c_str());
-    if (!dir)
-    {
-        Log::error("Failed to open models directory '%s'\n", dirName.c_str());
-        return;
-    }
-
-    struct dirent* entry = readdir(dir);
-    while (entry)
-    {
-        string pathname(dirName + "/");
-        pathname += string(entry->d_name);
-        // Skip '.' and '..'
-        if (entry->d_name[0] != '.')
-        {
-            fileVec.push_back(pathname);
-        }
-        entry = readdir(dir);
-    }
-    closedir(dir);
-}
-
 ModelMap modelMap;
-
 }
 
 const ModelMap&
@@ -642,10 +614,10 @@ 
     }
     vector<string> pathVec;
     string dataDir(GLMARK_DATA_PATH"/models");
-    ModelPrivate::list_files(dataDir, pathVec);
+    Util::list_files(dataDir, pathVec);
 #ifdef GLMARK_EXTRAS_PATH
     string extrasDir(GLMARK_EXTRAS_PATH"/models");
-    ModelPrivate::list_files(extrasDir, pathVec);
+    Util::list_files(extrasDir, pathVec);
 #endif
 
     // Now that we have a list of all of the model files available to us,

=== modified file 'src/util.cpp'
--- src/util.cpp	2011-08-10 17:58:47 +0000
+++ src/util.cpp	2011-10-13 13:56:26 +0000
@@ -25,6 +25,8 @@ 
 #include <fstream>
 #ifdef ANDROID
 #include <android/asset_manager.h>
+#else
+#include <dirent.h>
 #endif
 
 #include "log.h"
@@ -57,6 +59,31 @@ 
     return static_cast<std::istream *>(ifs);
 }
 
+void
+Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec)
+{
+    DIR* dir = opendir(dirName.c_str());
+    if (!dir)
+    {
+        Log::error("Failed to open models directory '%s'\n", dirName.c_str());
+        return;
+    }
+
+    struct dirent* entry = readdir(dir);
+    while (entry)
+    {
+        std::string pathname(dirName + "/");
+        pathname += std::string(entry->d_name);
+        // Skip '.' and '..'
+        if (entry->d_name[0] != '.')
+        {
+            fileVec.push_back(pathname);
+        }
+        entry = readdir(dir);
+    }
+    closedir(dir);
+}
+
 #else
 
 AAssetManager *Util::android_asset_manager = 0;
@@ -67,12 +94,19 @@ 
     Util::android_asset_manager = asset_manager;
 }
 
+AAssetManager *
+Util::android_get_asset_manager()
+{
+    return Util::android_asset_manager;
+}
+
 std::istream *
 Util::get_resource(const std::string &path)
 {
     std::string path2(path);
-    /* Remove leading '/' */
-    path2.erase(0, 1);
+    /* Remove leading '/' from path name, it confuses the AssetManager */
+    if (path2.size() > 0 && path2[0] == '/')
+        path2.erase(0, 1);
 
     std::stringstream *ss = new std::stringstream;
     AAsset *asset = AAssetManager_open(Util::android_asset_manager,
@@ -89,4 +123,31 @@ 
 
     return static_cast<std::istream *>(ss);
 }
+
+void
+Util::list_files(const std::string& dirName, std::vector<std::string>& fileVec)
+{
+    AAssetManager *mgr(Util::android_get_asset_manager());
+    std::string dir_name(dirName);
+
+    /* Remove leading '/' from path, it confuses the AssetManager */
+    if (dir_name.size() > 0 && dir_name[0] == '/')
+        dir_name.erase(0, 1);
+
+    AAssetDir* dir = AAssetManager_openDir(mgr, dir_name.c_str());
+    if (!dir)
+    {
+        Log::error("Failed to open models directory '%s'\n", dir_name.c_str());
+        return;
+    }
+
+    const char *filename(0);
+    while ((filename = AAssetDir_getNextFileName(dir)) != 0)
+    {
+        std::string pathname(dir_name + "/");
+        pathname += std::string(filename);
+        fileVec.push_back(pathname);
+    }
+    AAssetDir_close(dir);
+}
 #endif

=== modified file 'src/util.h'
--- src/util.h	2011-09-07 09:40:57 +0000
+++ src/util.h	2011-10-13 13:56:26 +0000
@@ -35,6 +35,7 @@ 
 struct Util {
     static void split(const std::string &s, char delim, std::vector<std::string> &elems);
     static std::istream *get_resource(const std::string &path);
+    static void list_files(const std::string& dirName, std::vector<std::string>& fileVec);
     template <class T> static void dispose_pointer_vector(std::vector<T*> &vec)
     {
         for (typename std::vector<T*>::const_iterator iter = vec.begin();
@@ -49,6 +50,7 @@ 
 
 #ifdef ANDROID
     static void android_set_asset_manager(AAssetManager *asset_manager);
+    static AAssetManager *android_get_asset_manager(void);
 private:
     static AAssetManager *android_asset_manager;
 #endif