| OLD | NEW |
| 1 // Copyright (c) 2012, the Dart project authors. Please see the AUTHORS file | 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 | 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. | 3 // BSD-style license that can be found in the LICENSE file. |
| 4 | 4 |
| 5 #include "embedders/android/graphics.h" | 5 #include "embedders/android/graphics.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 #include <GLES2/gl2ext.h> | 8 #include <GLES2/gl2ext.h> |
| 9 | 9 |
| 10 #include "embedders/android/log.h" | 10 #include "embedders/android/log.h" |
| 11 | 11 |
| 12 extern void CheckGLError(const char *function); |
| 13 |
| 12 Graphics::Graphics(android_app* application, Timer* timer) | 14 Graphics::Graphics(android_app* application, Timer* timer) |
| 13 : application_(application), | 15 : application_(application), |
| 14 timer_(timer), | 16 timer_(timer), |
| 15 width_(0), | 17 width_(0), |
| 16 height_(0), | 18 height_(0), |
| 17 display_(EGL_NO_DISPLAY), | 19 display_(EGL_NO_DISPLAY), |
| 18 surface_(EGL_NO_SURFACE), | 20 surface_(EGL_NO_SURFACE), |
| 19 context_(EGL_NO_CONTEXT) { | 21 context_(EGL_NO_CONTEXT) { |
| 20 } | 22 } |
| 21 | 23 |
| (...skipping 33 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 55 if (surface_ != EGL_NO_SURFACE) { | 57 if (surface_ != EGL_NO_SURFACE) { |
| 56 LOGI("eglCreateContext"); | 58 LOGI("eglCreateContext"); |
| 57 context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, | 59 context_ = eglCreateContext(display_, config, EGL_NO_CONTEXT, |
| 58 ctx_attribs); | 60 ctx_attribs); |
| 59 if (context_ != EGL_NO_CONTEXT) { | 61 if (context_ != EGL_NO_CONTEXT) { |
| 60 if (eglMakeCurrent(display_, surface_, surface_, context_) && | 62 if (eglMakeCurrent(display_, surface_, surface_, context_) && |
| 61 eglQuerySurface(display_, surface_, EGL_WIDTH, &width_) && | 63 eglQuerySurface(display_, surface_, EGL_WIDTH, &width_) && |
| 62 width_ > 0 && | 64 width_ > 0 && |
| 63 eglQuerySurface(display_, surface_, EGL_HEIGHT, &height_) && | 65 eglQuerySurface(display_, surface_, EGL_HEIGHT, &height_) && |
| 64 height_ > 0) { | 66 height_ > 0) { |
| 65 glViewport(0, 0, width_, height_); | 67 SetViewport(0, 0, width_, height_); |
| 66 return 0; | 68 return 0; |
| 67 } | 69 } |
| 68 } | 70 } |
| 69 } | 71 } |
| 70 } | 72 } |
| 71 } | 73 } |
| 72 } | 74 } |
| 73 } | 75 } |
| 74 LOGE("Error starting graphics"); | 76 LOGE("Error starting graphics"); |
| 75 Stop(); | 77 Stop(); |
| (...skipping 13 matching lines...) Expand all Loading... |
| 89 surface_ = EGL_NO_SURFACE; | 91 surface_ = EGL_NO_SURFACE; |
| 90 } | 92 } |
| 91 eglTerminate(display_); | 93 eglTerminate(display_); |
| 92 display_ = EGL_NO_DISPLAY; | 94 display_ = EGL_NO_DISPLAY; |
| 93 } | 95 } |
| 94 } | 96 } |
| 95 | 97 |
| 96 int32_t Graphics::Update() { | 98 int32_t Graphics::Update() { |
| 97 return 0; | 99 return 0; |
| 98 } | 100 } |
| 101 |
| 102 void Graphics::SwapBuffers() { |
| 103 EGLDisplay display = eglGetDisplay(EGL_DEFAULT_DISPLAY); |
| 104 EGLSurface surface = eglGetCurrentSurface(EGL_DRAW); |
| 105 eglSwapBuffers(display, surface); |
| 106 } |
| 107 |
| 108 void Graphics::SetViewport(int left, int top, int width, int height) { |
| 109 glViewport(left, top, width, height); |
| 110 CheckGLError("glViewPort"); |
| 111 } |
| 112 |
| 113 int Graphics::BuildProgram(const char* vertexShaderSource, |
| 114 const char* fragmentShaderSource) const { |
| 115 int vertexShader = BuildShader(vertexShaderSource, GL_VERTEX_SHADER); |
| 116 int fragmentShader = BuildShader(fragmentShaderSource, GL_FRAGMENT_SHADER); |
| 117 if (vertexShader < 0 || fragmentShader < 0) { |
| 118 return -1; |
| 119 } |
| 120 |
| 121 GLuint programHandle = glCreateProgram(); |
| 122 glAttachShader(programHandle, static_cast<GLuint>(vertexShader)); |
| 123 glAttachShader(programHandle, static_cast<GLuint>(fragmentShader)); |
| 124 glLinkProgram(programHandle); |
| 125 |
| 126 GLint linkSuccess; |
| 127 glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); |
| 128 if (linkSuccess == GL_FALSE) { |
| 129 GLint infoLogLength; |
| 130 glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 131 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
| 132 glGetProgramInfoLog(programHandle, infoLogLength, NULL, strInfoLog); |
| 133 strInfoLog[infoLogLength] = 0; |
| 134 LOGE("Link failed: %s", strInfoLog); |
| 135 delete[] strInfoLog; |
| 136 return -1; |
| 137 } |
| 138 return static_cast<int>(programHandle); |
| 139 } |
| 140 |
| 141 int Graphics::BuildShader(const char* source, GLenum shaderType) const { |
| 142 GLuint shaderHandle = glCreateShader(shaderType); |
| 143 glShaderSource(shaderHandle, 1, &source, NULL); |
| 144 glCompileShader(shaderHandle); |
| 145 |
| 146 GLint compileSuccess; |
| 147 glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess); |
| 148 |
| 149 if (compileSuccess == GL_FALSE) { |
| 150 GLint infoLogLength = 0; |
| 151 glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogLength); |
| 152 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; |
| 153 glGetShaderInfoLog(shaderHandle, infoLogLength, NULL, strInfoLog); |
| 154 strInfoLog[infoLogLength] = 0; |
| 155 LOGE("Shader compile failed: %s", strInfoLog); |
| 156 delete [] strInfoLog; |
| 157 return -1; |
| 158 } |
| 159 return static_cast<int>(shaderHandle); |
| 160 } |
| 161 |
| OLD | NEW |