如何在不创建GLSurfaceView的情况下检测OpenGL功能(Android) [英] How to detect OpenGL capabilities without creating a GLSurfaceView (Android)

查看:77
本文介绍了如何在不创建GLSurfaceView的情况下检测OpenGL功能(Android)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

在尝试针对图形目的使用OpenGL还是Canvas之前,我尝试访问手机的OpenGL功能.但是,我可以阅读文档的所有功能都要求您已经具有有效的OpenGL上下文(即,创建GLSurfaceView并为其分配渲染器.然后在onSurfaceCreated中检查OpenGL参数).

I am trying to access the OpenGL capability of the phone before deciding whether to use OpenGL or Canvas for graphics puposes. However, all the functions that I can read documentation on requires you to already have a valid OpenGL context (namely, create a GLSurfaceView and assign it a rendered. Then check the OpenGL parameters in the onSurfaceCreated).

那么,在必须创建任何OpenGL视图之前,有没有办法检查电话的扩展名,渲染器名称和最大纹理大小?

So, is there a way to check the extensions, renderer name and max texture size capability of the phone BEFORE having to create any OpenGL views?

推荐答案

搜索后,我得出的结论是,我需要一个有效的GL上下文才能查询其功能.这又需要一个Surface,依此类推.基本上,您需要先创建OpenGL表面,然后才能检查其支持什么.

After searching, I came to the conclusion that I need a valid GL context before I can query it for its capability. That, in turn requires a Surface, and so on. Basicly, you need to create the OpenGL surface before you can check what it supports.

这就是我最终要做的事情:我创建了一个新的活动(GraphicChooser,我需要使用我的班级名称……),而不是正常的活动.此活动将创建一个GLSurfaceView,它会检查onSurfaceCreated中设备的功能.根据找到的内容,它以有关要使用的图形选项的一些标志开始主要活动,然后退出.每个活动模式都设置为singleTask,因此退出一个不会影响另一个,并且每个活动只能有一个实例.例如,点击主页按钮并重新启动GraphicChooser活动之后,它将向主活动触发新的意图,该主活动仍处于活动状态,但不会创建新的意图.

So here is what I ended up doing: I created a new activity (GraphicChooser, I need to work on my classes names...) that is launched instead of my normal activity. This activity creates a GLSurfaceView, which check the capability of the device in onSurfaceCreated. Depending on what is found, it starts the main activity with some flags about the graphic options to use, then exits. Each activity mode is set to singleTask so quitting one doesn't affect the other and there can only be a single instance of each. For example, after hitting the home button and you restart the GraphicChooser activity, it will fire a new intent to main activity, which is still active but will not create a new one.

它非常粗糙,当然还有更好的方法,但是我找不到.主要缺点是,每次启动该活动时,创建一个额外的活动都会产生开销.

It is very crude and there certainly is a better way to do it, but I couldn't find it. The main downside is that each time you start the activity, there is the overhead of creating an extra one.

package com.greencod.pinball.android;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.app.Activity;
import android.content.Intent;
import android.opengl.GLSurfaceView;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;

public class GraphicChooser extends Activity {

   private GLSurfaceView mGLView;

   @Override
   protected void onCreate(Bundle savedInstanceState) {
       super.onCreate(savedInstanceState);

       Log.d("Graphic Chooser", "onCreate: create view and renderer.");
       // get the screen size
       Display display = getWindowManager().getDefaultDisplay();
       int width = display.getWidth();
       int height = display.getHeight();

       mGLView = new GLSurfaceView(this);
       mGLView.setRenderer(new GraphicChooserRenderer(this, width, height));
       setContentView(mGLView);
   }

   @Override
   protected void onResume() {
      super.onResume();

      Log.d("Graphic Chooser", "onResume: purely for testing purpose.");
   }

   @Override
   protected void onDestroy() {
      super.onDestroy();

      Log.d("Graphic Chooser", "onDestroy: Bye bye.");
   }

   static final int GAME_ACTIVITY_REQUEST_CODE = 10;
   public void launchGraphics(int type)
   {
      // launch game activity and kill this activity
      Intent i = new Intent(this, PinballActivity.class);
      i.putExtra("Graphics", type);
      // the activity requested should go in a new task, so even if we are passing
      // a request code, we will not get it when the new activity stops, but right now
      // as a 'cancel' request. That is ok, just quit this activity then.
      startActivityForResult(i, GAME_ACTIVITY_REQUEST_CODE);
   }

   @Override
   protected void onActivityResult(int requestCode, int resultCode, Intent data) {
      super.onActivityResult(requestCode, resultCode, data);
      if( requestCode == GAME_ACTIVITY_REQUEST_CODE )
         finish();
   }
}

class GraphicChooserRenderer implements GLSurfaceView.Renderer{

   GraphicChooser _activity;
   final int _width, _height;
   public GraphicChooserRenderer( GraphicChooser activity, int width, int height )
   {
      _activity = activity;
      _width = width;
      _height = height;
   }

   final int GRAPHICS_CANVAS = 0;
   final int GRAPHICS_OPENGL_DRAW_TEXTURE = 1;

   public void determineGraphicSupport(GL10 gl)
   {
      int _graphicEngine = GRAPHICS_CANVAS;

      String extensions = gl.glGetString(GL10.GL_EXTENSIONS); 
//      String version = GLES10.glGetString(GL10.GL_VERSION);
      String renderer = gl.glGetString(GL10.GL_RENDERER);
      boolean isSoftwareRenderer = renderer.contains("PixelFlinger");
      boolean supportsDrawTexture = extensions.contains("draw_texture");
      int[] arGlMaxTextureSize = new int[1];
      gl.glGetIntegerv( GL10.GL_MAX_TEXTURE_SIZE, arGlMaxTextureSize, 0 );

      if( !isSoftwareRenderer && supportsDrawTexture && _width >= 480 && _height >= 800 
            && arGlMaxTextureSize[0] >= 2048 )
         _graphicEngine = GRAPHICS_OPENGL_DRAW_TEXTURE;
      else
         _graphicEngine = GRAPHICS_CANVAS;

      Log.i("pinball", "Graphics using " + (_graphicEngine==GRAPHICS_CANVAS?"Canvas":"OpenGL EOS draw texture")
            + ". OpenGL renderer: " + renderer
            + ". Software renderer: " + isSoftwareRenderer
            + ". Support draw texture: " + supportsDrawTexture 
            + ". Texture max size: " + arGlMaxTextureSize[0]
            + ". Resolution: " + _width + "x" + _height );

      _activity.launchGraphics(_graphicEngine);
   }

   public void onSurfaceCreated(GL10 gl, EGLConfig config) {
      determineGraphicSupport(gl);
   }

   public void onSurfaceChanged(GL10 gl, int w, int h) {
      gl.glViewport(0, 0, w, h);
   }

   public void onDrawFrame(GL10 gl) {
      gl.glClear(GL10.GL_COLOR_BUFFER_BIT | GL10.GL_DEPTH_BUFFER_BIT);
   }  
}

这篇关于如何在不创建GLSurfaceView的情况下检测OpenGL功能(Android)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆