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