Hello,
Welcome to our Microsoft Q&A platform!
You can use OpenGL to get GPU information in Xamarin Android(Note: please run following code to android device).
Firstly, we can write a class to implement GLSurfaceView.IRenderer
like following code. We can get GPU information in the OnSurfaceCreated
method.
class MySurface : Java.Lang.Object, GLSurfaceView.IRenderer
{
private TextView tvVendor;
RelativeLayout rlRoot;
GLSurfaceView mGlSurfaceView;
public MySurface(TextView tvVendor, RelativeLayout rlRoot, GLSurfaceView mGlSurfaceView)
{
this.tvVendor = tvVendor;
this.rlRoot = rlRoot;
this.mGlSurfaceView = mGlSurfaceView;
}
public void OnDrawFrame(IGL10 gl)
{
gl.GlClear(GL10.GlColorBufferBit);
}
public void OnSurfaceChanged(IGL10 gl, int width, int height)
{
}
public void OnSurfaceCreated(IGL10 gl, Javax.Microedition.Khronos.Egl.EGLConfig config)
{
string extensions = GLES20.GlGetString(GLES20.GlExtensions);
var red = gl.GlGetString(GL10.GlRenderer);
string GPUInfo = "gl renderer: " + gl.GlGetString(GL10.GlRenderer) + "\n gl vendor: " + gl.GlGetString(GL10.GlVendor);
MainThread.BeginInvokeOnMainThread(() => {
tvVendor.Text = GPUInfo;
rlRoot.RemoveView(mGlSurfaceView);
});
}
}
Then I test it in the OnCreate
method of MainActivity.cs
, we can show the GPU vendor, model or renderer in the textview
, If you do not need to do that, you can store the GPU information to the sharedpreferences
.
public class MainActivity : AppCompatActivity
{
TextView tvVendor;
RelativeLayout rlRoot;
GLSurfaceView mGlSurfaceView;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
rlRoot = (RelativeLayout)FindViewById(Resource.Id.rlRoot);
tvVendor = (TextView)FindViewById(Resource.Id.tvVendor);
mGlSurfaceView = new GLSurfaceView(this);
mGlSurfaceView.SetEGLContextClientVersion(3);
mGlSurfaceView.SetEGLConfigChooser(8, 8, 8, 8, 0, 0);
mGlSurfaceView.SetRenderer(new MySurface(tvVendor, rlRoot, mGlSurfaceView));
rlRoot.AddView(mGlSurfaceView);
}
public override void OnRequestPermissionsResult(int requestCode, string[] permissions, [GeneratedEnum] Android.Content.PM.Permission[] grantResults)
{
Xamarin.Essentials.Platform.OnRequestPermissionsResult(requestCode, permissions, grantResults);
base.OnRequestPermissionsResult(requestCode, permissions, grantResults);
}
}
Here is activity_main.xml
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
xmlns:app="http://schemas.android.com/apk/res-auto"
xmlns:tools="http://schemas.android.com/tools"
android:id="@+id/rlRoot"
android:layout_width="match_parent"
android:layout_height="match_parent">
<TextView
android:id="@+id/tvVendor"
android:layout_width="wrap_content"
android:layout_height="wrap_content"
android:textColor="#FF0000"
android:text="test"
/>
</RelativeLayout>
======================
Updated==============================
need informaction about CPU like name, number of core or frequency...
You can read "/system/bin/cat", "/proc/cpuinfo"
path to get CPU information, please use following code.
using Android.App;
using Android.OS;
using Android.Support.V7.App;
using Android.Runtime;
using Android.Widget;
using Java.Lang;
using Java.IO;
using System.IO;
using System.Text;
namespace App55
{
[Activity(Label = "@string/app_name", Theme = "@style/AppTheme", MainLauncher = true)]
public class MainActivity : AppCompatActivity
{
ProcessBuilder processBuilder;
string[] DATA = { "/system/bin/cat", "/proc/cpuinfo" };
Java.Lang.Process process;
protected override void OnCreate(Bundle savedInstanceState)
{
base.OnCreate(savedInstanceState);
Xamarin.Essentials.Platform.Init(this, savedInstanceState);
// Set our view from the "main" layout resource
SetContentView(Resource.Layout.activity_main);
TextView textView1 = FindViewById<TextView>(Resource.Id.textView1);
string value;
try
{
processBuilder = new ProcessBuilder(DATA);
process = processBuilder.Start();
Stream inputStream = process.InputStream;
using (var reader = new StreamReader(inputStream, Encoding.UTF8))
{
value = reader.ReadToEnd();
}
textView1.Text = value;
}
catch (Java.IO.IOException ex)
{
ex.PrintStackTrace();
}
}
Best Regards,
Leon Lu
If the response is helpful, please click "Accept Answer" and upvote it.
Note: Please follow the steps in our documentation to enable e-mail notifications if you want to receive the related email notification for this thread.