Home | History | Annotate | Download | only in graphics
      1 /*
      2  * Copyright (C) 2009 The Android Open Source Project
      3  *
      4  * Licensed under the Apache License, Version 2.0 (the "License");
      5  * you may not use this file except in compliance with the License.
      6  * You may obtain a copy of the License at
      7  *
      8  *      http://www.apache.org/licenses/LICENSE-2.0
      9  *
     10  * Unless required by applicable law or agreed to in writing, software
     11  * distributed under the License is distributed on an "AS IS" BASIS,
     12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
     13  * See the License for the specific language governing permissions and
     14  * limitations under the License.
     15  */
     16 
     17 package com.example.android.apis.graphics;
     18 
     19 import android.app.Activity;
     20 import android.app.ActivityManager;
     21 import android.content.Context;
     22 import android.content.pm.ConfigurationInfo;
     23 import android.opengl.GLSurfaceView;
     24 import android.os.Bundle;
     25 
     26 /**
     27  * This sample shows how to check for OpenGL ES 2.0 support at runtime, and then
     28  * use either OpenGL ES 1.0 or OpenGL ES 2.0, as appropriate.
     29  */
     30 public class GLES20Activity extends Activity {
     31     @Override
     32     protected void onCreate(Bundle savedInstanceState) {
     33         super.onCreate(savedInstanceState);
     34         mGLSurfaceView = new GLSurfaceView(this);
     35         if (detectOpenGLES20()) {
     36             // Tell the surface view we want to create an OpenGL ES 2.0-compatible
     37             // context, and set an OpenGL ES 2.0-compatible renderer.
     38             mGLSurfaceView.setEGLContextClientVersion(2);
     39             mGLSurfaceView.setRenderer(new GLES20TriangleRenderer(this));
     40         } else {
     41             // Set an OpenGL ES 1.x-compatible renderer. In a real application
     42             // this renderer might approximate the same output as the 2.0 renderer.
     43             mGLSurfaceView.setRenderer(new TriangleRenderer(this));
     44         }
     45         setContentView(mGLSurfaceView);
     46     }
     47 
     48     private boolean detectOpenGLES20() {
     49         ActivityManager am =
     50             (ActivityManager) getSystemService(Context.ACTIVITY_SERVICE);
     51         ConfigurationInfo info = am.getDeviceConfigurationInfo();
     52         return (info.reqGlEsVersion >= 0x20000);
     53     }
     54 
     55     @Override
     56     protected void onResume() {
     57         // Ideally a game should implement onResume() and onPause()
     58         // to take appropriate action when the activity looses focus
     59         super.onResume();
     60         mGLSurfaceView.onResume();
     61     }
     62 
     63     @Override
     64     protected void onPause() {
     65         // Ideally a game should implement onResume() and onPause()
     66         // to take appropriate action when the activity looses focus
     67         super.onPause();
     68         mGLSurfaceView.onPause();
     69     }
     70 
     71     private GLSurfaceView mGLSurfaceView;
     72 }
     73