Home | History | Annotate | Download | only in ndk_helper
      1 /*
      2  * Copyright 2013 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 //--------------------------------------------------------------------------------
     18 // GLContext.h
     19 //--------------------------------------------------------------------------------
     20 #ifndef GLCONTEXT_H_
     21 #define GLCONTEXT_H_
     22 
     23 #include <EGL/egl.h>
     24 #include <GLES2/gl2.h>
     25 #include <android/log.h>
     26 
     27 #include "JNIHelper.h"
     28 
     29 namespace ndk_helper
     30 {
     31 
     32 //--------------------------------------------------------------------------------
     33 // Constants
     34 //--------------------------------------------------------------------------------
     35 
     36 //--------------------------------------------------------------------------------
     37 // Class
     38 //--------------------------------------------------------------------------------
     39 
     40 /******************************************************************
     41  * OpenGL context handler
     42  * The class handles OpenGL and EGL context based on Android activity life cycle
     43  * The caller needs to call corresponding methods for each activity life cycle events as it's done in sample codes.
     44  *
     45  * Also the class initializes OpenGL ES3 when the compatible driver is installed in the device.
     46  * getGLVersion() returns 3.0~ when the device supports OpenGLES3.0
     47  *
     48  * Thread safety: OpenGL context is expecting used within dedicated single thread,
     49  * thus GLContext class is not designed as a thread-safe
     50  */
     51 class GLContext
     52 {
     53 private:
     54     //EGL configurations
     55     ANativeWindow* window_;
     56     EGLDisplay display_;
     57     EGLSurface surface_;
     58     EGLContext context_;
     59     EGLConfig config_;
     60 
     61     //Screen parameters
     62     int32_t screen_width_;
     63     int32_t screen_height_;
     64     int32_t color_size_;
     65     int32_t depth_size_;
     66 
     67     //Flags
     68     bool gles_initialized_;
     69     bool egl_context_initialized_;
     70     bool es3_supported_;
     71     float gl_version_;
     72     bool context_valid_;
     73 
     74     void InitGLES();
     75     void Terminate();
     76     bool InitEGLSurface();
     77     bool InitEGLContext();
     78 
     79     GLContext( GLContext const& );
     80     void operator=( GLContext const& );
     81     GLContext();
     82     virtual ~GLContext();
     83 public:
     84     static GLContext* GetInstance()
     85     {
     86         //Singleton
     87         static GLContext instance;
     88 
     89         return &instance;
     90     }
     91 
     92     bool Init( ANativeWindow* window );
     93     EGLint Swap();
     94     bool Invalidate();
     95 
     96     void Suspend();
     97     EGLint Resume( ANativeWindow* window );
     98 
     99     int32_t GetScreenWidth()
    100     {
    101         return screen_width_;
    102     }
    103     int32_t GetScreenHeight()
    104     {
    105         return screen_height_;
    106     }
    107 
    108     int32_t GetBufferColorSize()
    109     {
    110         return color_size_;
    111     }
    112     int32_t GetBufferDepthSize()
    113     {
    114         return depth_size_;
    115     }
    116     float GetGLVersion()
    117     {
    118         return gl_version_;
    119     }
    120     bool CheckExtension( const char* extension );
    121 };
    122 
    123 }   //namespace ndkHelper
    124 
    125 #endif /* GLCONTEXT_H_ */
    126