| OLD | NEW |
| 1 // Copyright (c) 2013, the Dart project authors. Please see the AUTHORS file | 1 // Copyright (c) 2013, 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/openglui/common/gl_graphics_handler.h" | 5 #include "embedders/openglui/common/graphics_handler.h" |
| 6 #include "embedders/openglui/common/canvas_context.h" |
| 6 #include "embedders/openglui/common/log.h" | 7 #include "embedders/openglui/common/log.h" |
| 7 | 8 |
| 8 extern void CheckGLError(const char *function); | 9 extern void CheckGLError(const char *function); |
| 9 | 10 |
| 10 void GLGraphicsHandler::SetViewport(int left, int top, int width, int height) { | 11 GraphicsHandler* graphics; |
| 12 |
| 13 GraphicsHandler::GraphicsHandler() |
| 14 : ag(), |
| 15 grcontext(NULL), |
| 16 width_(0), |
| 17 height_(0) { |
| 18 graphics = this; |
| 19 } |
| 20 |
| 21 // Kludge to get around an issue with Android emulator, which returns |
| 22 // NULL for the shader language version, causing Skia to blow up. |
| 23 const GrGLubyte* myGLGetString(GLenum name) { |
| 24 const GrGLubyte* str = glGetString(name); |
| 25 if (NULL == str && GL_SHADING_LANGUAGE_VERSION == name) { |
| 26 return reinterpret_cast<GrGLubyte*>( |
| 27 const_cast<char*>("OpenGL ES GLSL ES 1.0")); |
| 28 } else { |
| 29 return str; |
| 30 } |
| 31 } |
| 32 |
| 33 int32_t GraphicsHandler::Start() { |
| 34 SkGraphics::Init(); |
| 35 GrGLInterface* fGL = const_cast<GrGLInterface*>(GrGLCreateNativeInterface()); |
| 36 LOGI("Created native interface %s\n", fGL ? "succeeded" : "failed"); |
| 37 fGL->fGetString = myGLGetString; |
| 38 grcontext = GrContext::Create(kOpenGL_Shaders_GrEngine, |
| 39 (GrPlatform3DContext)fGL); |
| 40 LOGI("Created GrContext %s\n", grcontext ? "succeeded" : "failed"); |
| 41 return 0; |
| 42 } |
| 43 |
| 44 void GraphicsHandler::Stop() { |
| 45 SkGraphics::Term(); |
| 46 } |
| 47 |
| 48 SkCanvas* GraphicsHandler::CreateCanvas() { |
| 49 GrPlatformRenderTargetDesc desc; |
| 50 desc.fWidth = width_; |
| 51 desc.fHeight = height_; |
| 52 desc.fConfig = kSkia8888_PM_GrPixelConfig; |
| 53 glGetIntegerv(GL_SAMPLES, &desc.fSampleCnt); |
| 54 glGetIntegerv(GL_STENCIL_BITS, &desc.fStencilBits); |
| 55 GrGLint buffer; |
| 56 glGetIntegerv(GL_FRAMEBUFFER_BINDING, &buffer); |
| 57 desc.fRenderTargetHandle = buffer; |
| 58 GrRenderTarget* fGrRenderTarget = grcontext->createPlatformRenderTarget(desc); |
| 59 return new SkCanvas(new SkGpuDevice(grcontext, fGrRenderTarget)); |
| 60 } |
| 61 |
| 62 void GraphicsHandler::SetViewport(int left, int top, int width, int height) { |
| 63 width_ = width; |
| 64 height_ = height; |
| 11 glViewport(left, top, width, height); | 65 glViewport(left, top, width, height); |
| 12 CheckGLError("glViewPort"); | 66 CheckGLError("glViewPort"); |
| 13 } | 67 } |
| 14 | 68 |
| 15 int GLGraphicsHandler::BuildProgram(const char* vertexShaderSource, | 69 int32_t GraphicsHandler::Update() { |
| 16 const char* fragmentShaderSource) const { | 70 extern CanvasContext* display_context; |
| 17 int vertexShader = BuildShader(vertexShaderSource, GL_VERTEX_SHADER); | 71 if (display_context != NULL) { |
| 18 int fragmentShader = BuildShader(fragmentShaderSource, GL_FRAGMENT_SHADER); | 72 LOGI("Flushing display context\n"); |
| 19 if (vertexShader < 0 || fragmentShader < 0) { | 73 display_context->Flush(); |
| 20 return -1; | |
| 21 } | 74 } |
| 22 | 75 SwapBuffers(); |
| 23 GLuint programHandle = glCreateProgram(); | 76 return 0; |
| 24 glAttachShader(programHandle, static_cast<GLuint>(vertexShader)); | |
| 25 glAttachShader(programHandle, static_cast<GLuint>(fragmentShader)); | |
| 26 glLinkProgram(programHandle); | |
| 27 | |
| 28 GLint linkSuccess; | |
| 29 glGetProgramiv(programHandle, GL_LINK_STATUS, &linkSuccess); | |
| 30 if (linkSuccess == GL_FALSE) { | |
| 31 GLint infoLogLength; | |
| 32 glGetProgramiv(programHandle, GL_INFO_LOG_LENGTH, &infoLogLength); | |
| 33 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
| 34 glGetProgramInfoLog(programHandle, infoLogLength, NULL, strInfoLog); | |
| 35 strInfoLog[infoLogLength] = 0; | |
| 36 LOGE("Link failed: %s", strInfoLog); | |
| 37 delete[] strInfoLog; | |
| 38 return -1; | |
| 39 } | |
| 40 return static_cast<int>(programHandle); | |
| 41 } | 77 } |
| 42 | 78 |
| 43 int GLGraphicsHandler::BuildShader(const char* source, | |
| 44 GLenum shaderType) const { | |
| 45 GLuint shaderHandle = glCreateShader(shaderType); | |
| 46 glShaderSource(shaderHandle, 1, &source, NULL); | |
| 47 glCompileShader(shaderHandle); | |
| 48 | 79 |
| 49 GLint compileSuccess; | |
| 50 glGetShaderiv(shaderHandle, GL_COMPILE_STATUS, &compileSuccess); | |
| 51 | 80 |
| 52 if (compileSuccess == GL_FALSE) { | |
| 53 GLint infoLogLength = 0; | |
| 54 glGetShaderiv(shaderHandle, GL_INFO_LOG_LENGTH, &infoLogLength); | |
| 55 GLchar* strInfoLog = new GLchar[infoLogLength + 1]; | |
| 56 glGetShaderInfoLog(shaderHandle, infoLogLength, NULL, strInfoLog); | |
| 57 strInfoLog[infoLogLength] = 0; | |
| 58 LOGE("Shader compile failed: %s", strInfoLog); | |
| 59 delete [] strInfoLog; | |
| 60 return -1; | |
| 61 } | |
| 62 return static_cast<int>(shaderHandle); | |
| 63 } | |
| 64 | 81 |
| OLD | NEW |