android 获取gpu 内存使用率
时间: 2023-11-13 13:02:33 浏览: 218
Testin公开课第九期-Andriod性能测试初探(李毅)
5星 · 资源好评率100%
可以通过使用Android系统提供的OpenGL ES API来获取GPU内存使用率。
1. 首先,在AndroidManifest.xml文件中添加以下权限:
```xml
<uses-permission android:name="android.permission.READ_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
<uses-permission android:name="android.permission.GET_TASKS"/>
<uses-permission android:name="android.permission.REAL_GET_TASKS"/>
```
2. 在代码中,可以使用如下方式获取OpenGL ES的状态信息:
```java
import android.app.ActivityManager;
import android.content.Context;
import android.opengl.GLSurfaceView;
import javax.microedition.khronos.opengles.GL10;
public class OpenGLMonitor implements GLSurfaceView.Renderer {
private ActivityManager mActivityManager;
private int mMaxTextureSize;
private int mMaxTextureUnits;
private int mMaxVertexAttribs;
private int mMaxVaryingVectors;
private int mMaxVertexUniformComponents;
private int mMaxFragmentUniformComponents;
private int mMaxVertexTextureImageUnits;
private int mMaxFragmentTextureImageUnits;
private int mMaxRenderbufferSize;
private int mMaxViewportDims;
public OpenGLMonitor(Context context) {
mActivityManager = (ActivityManager)context.getSystemService(Context.ACTIVITY_SERVICE);
}
@Override
public void onSurfaceCreated(GL10 gl, javax.microedition.khronos.egl.EGLConfig config) {
mMaxTextureSize = getMaxTextureSize(gl);
mMaxTextureUnits = getMaxTextureUnits(gl);
mMaxVertexAttribs = getMaxVertexAttribs(gl);
mMaxVaryingVectors = getMaxVaryingVectors(gl);
mMaxVertexUniformComponents = getMaxVertexUniformComponents(gl);
mMaxFragmentUniformComponents = getMaxFragmentUniformComponents(gl);
mMaxVertexTextureImageUnits = getMaxVertexTextureImageUnits(gl);
mMaxFragmentTextureImageUnits = getMaxFragmentTextureImageUnits(gl);
mMaxRenderbufferSize = getMaxRenderbufferSize(gl);
mMaxViewportDims = getMaxViewportDims(gl);
}
@Override
public void onSurfaceChanged(GL10 gl, int width, int height) {
}
@Override
public void onDrawFrame(GL10 gl) {
}
private int getMaxTextureSize(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_SIZE, params, 0);
return params[0];
}
private int getMaxTextureUnits(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_UNITS, params, 0);
return params[0];
}
private int getMaxVertexAttribs(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_VERTEX_ATTRIBS, params, 0);
return params[0];
}
private int getMaxVaryingVectors(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_VARYING_VECTORS, params, 0);
return params[0];
}
private int getMaxVertexUniformComponents(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_VERTEX_UNIFORM_COMPONENTS, params, 0);
return params[0];
}
private int getMaxFragmentUniformComponents(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_FRAGMENT_UNIFORM_COMPONENTS, params, 0);
return params[0];
}
private int getMaxVertexTextureImageUnits(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS, params, 0);
return params[0];
}
private int getMaxFragmentTextureImageUnits(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_TEXTURE_IMAGE_UNITS, params, 0);
return params[0];
}
private int getMaxRenderbufferSize(GL10 gl) {
int[] params = new int[1];
gl.glGetIntegerv(GL10.GL_MAX_RENDERBUFFER_SIZE, params, 0);
return params[0];
}
private int getMaxViewportDims(GL10 gl) {
int[] params = new int[2];
gl.glGetIntegerv(GL10.GL_MAX_VIEWPORT_DIMS, params, 0);
return params[0] * params[1];
}
public float getGpuUsage() {
ActivityManager.MemoryInfo memoryInfo = new ActivityManager.MemoryInfo();
mActivityManager.getMemoryInfo(memoryInfo);
long totalMemory = memoryInfo.totalMem;
long availMemory = memoryInfo.availMem;
long usedMemory = totalMemory - availMemory;
float usage = (float) usedMemory / totalMemory;
return usage;
}
}
```
3. 然后,可以在需要获取GPU内存使用率的地方调用getGpuUsage()方法来获取GPU内存使用率。
```java
OpenGLMonitor monitor = new OpenGLMonitor(context);
float gpuUsage = monitor.getGpuUsage();
```
阅读全文