| OLD | NEW |
| (Empty) |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | |
| 2 // for details. All rights reserved. Use of this source code is governed by a | |
| 3 // BSD-style license that can be found in the LICENSE file. | |
| 4 | |
| 5 #include "jni/graphics.h" | |
| 6 | |
| 7 #include <GLES2/gl2.h> | |
| 8 #include <GLES2/gl2ext.h> | |
| 9 | |
| 10 #include "jni/log.h" | |
| 11 | |
| 12 Graphics::Graphics(android_app* application, Timer* timer) | |
| 13 : application_(application), | |
| 14 timer_(timer), | |
| 15 width_(0), | |
| 16 height_(0), | |
| 17 display_(EGL_NO_DISPLAY), | |
| 18 surface_(EGL_NO_SURFACE), | |
| 19 context_(EGL_NO_CONTEXT) { | |
| 20 } | |
| 21 | |
| 22 const int32_t& Graphics::height() { | |
| 23 return height_; | |
| 24 } | |
| 25 | |
| 26 const int32_t& Graphics::width() { | |
| 27 return width_; | |
| 28 } | |
| 29 | |
| 30 int32_t Graphics::Start() { | |
| 31 EGLint format, numConfigs, errorResult; | |
| 32 EGLConfig config; | |
| 33 const EGLint attributes[] = { | |
| 34 EGL_RENDERABLE_TYPE, EGL_OPENGL_ES2_BIT, | |
| 35 EGL_NONE | |
| 36 }; | |
| 37 static const EGLint ctx_attribs[] = { | |
| 38 EGL_CONTEXT_CLIENT_VERSION, 2, | |
| 39 EGL_NONE | |
| 40 }; | |
| 41 | |
| 42 display_ = eglGetDisplay(EGL_DEFAULT_DISPLAY); | |
| 43 if (display_ != EGL_NO_DISPLAY) { | |
| 44 LOGI("eglInitialize"); | |
| 45 if (eglInitialize(display_, NULL, NULL)) { | |
| 46 LOGI("eglChooseConfig"); | |
| 47 if (eglChooseConfig(display_, attributes, &config, 1, &numConfigs) && | |
| 48 numConfigs > 0) { | |
| 49 LOGI("eglGetConfigAttrib"); | |
| 50 if (eglGetConfigAttrib(display_, config, | |
| 51 EGL_NATIVE_VISUAL_ID, &format)) { | |
| 52 ANativeWindow_setBuffersGeometry(application_->window, 0, 0, format); | |
| 53 surface_ = eglCreateWindowSurface(display_, config, | |
| 54 (EGLNativeWindowType)application_->window, NULL); | |
| 55 if (surface_ != EGL_NO_SURFACE) { | |
| 56 LOGI("eglCreateContext"); | |
| 57 context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, | |
| 58 ctx_attribs); | |
| 59 if (context_ != EGL_NO_CONTEXT) { | |
| 60 if (eglMakeCurrent(display_, surface_, surface_, context_) && | |
| 61 eglQuerySurface(display_, surface_, EGL_WIDTH, &width_) && | |
| 62 width_ > 0 && | |
| 63 eglQuerySurface(display_, surface_, EGL_HEIGHT, &height_) && | |
| 64 height_ > 0) { | |
| 65 glViewport(0, 0, width_, height_); | |
| 66 return 0; | |
| 67 } | |
| 68 } | |
| 69 } | |
| 70 } | |
| 71 } | |
| 72 } | |
| 73 } | |
| 74 LOGE("Error starting graphics"); | |
| 75 Stop(); | |
| 76 return -1; | |
| 77 } | |
| 78 | |
| 79 void Graphics::Stop() { | |
| 80 LOGI("Stopping graphics"); | |
| 81 if (display_ != EGL_NO_DISPLAY) { | |
| 82 eglMakeCurrent(display_, EGL_NO_SURFACE, EGL_NO_SURFACE, EGL_NO_CONTEXT); | |
| 83 if (context_ != EGL_NO_CONTEXT) { | |
| 84 eglDestroyContext(display_, context_); | |
| 85 context_ = EGL_NO_CONTEXT; | |
| 86 } | |
| 87 if (surface_ != EGL_NO_SURFACE) { | |
| 88 eglDestroySurface(display_, surface_); | |
| 89 surface_ = EGL_NO_SURFACE; | |
| 90 } | |
| 91 eglTerminate(display_); | |
| 92 display_ = EGL_NO_DISPLAY; | |
| 93 } | |
| 94 } | |
| 95 | |
| 96 int32_t Graphics::Update() { | |
| 97 return 0; | |
| 98 } | |
| OLD | NEW |