OpenGL ES SDK for Android ARM Developer Center
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
AndroidPlatform.cpp
Go to the documentation of this file.
1 /* Copyright (c) 2012-2017, ARM Limited and Contributors
2  *
3  * SPDX-License-Identifier: MIT
4  *
5  * Permission is hereby granted, free of charge,
6  * to any person obtaining a copy of this software and associated documentation files (the "Software"),
7  * to deal in the Software without restriction, including without limitation the rights to
8  * use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software,
9  * and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
10  *
11  * The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
12  *
13  * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED,
14  * INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
15  * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
16  * IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY,
17  * WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
18  * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
19  */
20 
21 #include <string>
22 
23 #include <android/log.h>
24 
25 #include <GLES2/gl2.h>
26 
27 #include "AndroidPlatform.h"
28 #include "JavaClass.h"
29 
30 #include <unistd.h>
31 
32 using std::string;
33 
34 namespace MaliSDK
35 {
43  void AndroidPlatform::checkGlesError(const char* operation)
44  {
45  GLint error = 0;
46  for (error = glGetError(); error != GL_NO_ERROR; error = glGetError())
47  {
48  LOGE("glError (0x%x) after `%s` \n", error, operation);
49  LOGE("glError (0x%x) = `%s` \n", error, glErrorToString(error));
50  }
51  }
52 
56  const char* AndroidPlatform::glErrorToString(int glErrorCode)
57  {
58  switch(glErrorCode)
59  {
60  case GL_NO_ERROR:
61  return "GL_NO_ERROR";
62  break;
63  case GL_INVALID_ENUM:
64  return "GL_INVALID_ENUM";
65  break;
66  case GL_INVALID_VALUE:
67  return "GL_INVALID_VALUE";
68  break;
69  case GL_INVALID_OPERATION:
70  return "GL_INVALID_OPERATION";
71  break;
72  case GL_OUT_OF_MEMORY:
73  return "GL_OUT_OF_MEMORY";
74  break;
75  case GL_INVALID_FRAMEBUFFER_OPERATION:
76  return "GL_INVALID_FRAMEBUFFER_OPERATION";
77  break;
78  default:
79  return "unknown";
80  break;
81  }
82  }
83 
84  char* AndroidPlatform::copyString(const char* string)
85  {
86  int length = 0;
87  char *newString = NULL;
88  if (string == NULL)
89  {
90  return NULL;
91  }
92  length = strlen(string) + 1;
93  newString = (char*)malloc(length);
94  if (newString == NULL)
95  {
96  LOGE("copyString(): Failed to allocate memory using malloc().\n");
97  return NULL;
98  }
99  memcpy(newString, string, length);
100  return newString;
101  }
102 
103  bool AndroidPlatform::getAndroidAsset(JNIEnv* JNIEnvironment, const char destinationDirectory[], const char filename[])
104  {
105  if (JNIEnvironment == NULL || destinationDirectory == NULL || filename == NULL)
106  {
107  LOGE("getAndroidAsset(): NULL argument is not acceptable.\n");
108  return false;
109  }
110 
111  /* Create the full path to where we want the file to be found. */
112  string resourceFilePath = string(destinationDirectory) + string(filename);
113 
114  /* Try and find the file in the file system. */
115  FILE * file = NULL;
116  file = fopen(resourceFilePath.c_str(), "r");
117  if (file != NULL)
118  {
119  /*
120  * The file does exist on the target device's file system,
121  * The program can use this file as normal.
122  */
123  fclose (file);
124  }
125  else
126  {
127  /* The file does not exist and needs to be extracted from the APK package */
128 
129  /* Use the MaliSamplesActivity.extractAsset() Java method to extract the file */
130  JavaClass javaClass(JNIEnvironment, "com/arm/malideveloper/openglessdk/MaliSamplesActivity");
131 
132  /* Extract the file from the asset folder embedded in the APK to the destination directory. */
133  if (!javaClass.staticMethod("extractAsset", destinationDirectory, filename))
134  {
135  LOGE("getAndroidAsset(): Failed to call MaliSamplesActivity.extractAsset() for %s\n", filename);
136  return false;
137  }
138  }
139  return true;
140  }
141 }
static const char * glErrorToString(int glErrorCode)
Converts OpenGL ES error codes into the readable strings.
static char * copyString(const char *string)
Deep copy a string using memcopy().
Wraps a Java class to allow access to it's static fields and methods using JNI.
Definition: JavaClass.h:42
static void checkGlesError(const char *operation)
Checks if OpenGL ES has reported any errors.
static bool getAndroidAsset(JNIEnv *JNIEnvironment, const char destinationDirectory[], const char filename[])
Extract an asset file from the APK.
GLenum GLuint GLenum GLsizei length
Definition: gl2ext.h:134
#define LOGE(...)
Definition: AstcTextures.h:30
bool staticMethod(const char *methodName, int **returnValue, const char *param01)
Call a static method with one parameter which returns an integer array within the Java class...
Definition: JavaClass.cpp:92