| OLD | NEW |
| 1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "mojo/apps/js/bindings/gl/context.h" | 5 #include "mojo/apps/js/bindings/gl/context.h" |
| 6 | 6 |
| 7 #include <GLES2/gl2.h> | 7 #include <GLES2/gl2.h> |
| 8 | 8 |
| 9 #include "gin/arguments.h" | 9 #include "gin/arguments.h" |
| 10 #include "gin/object_template_builder.h" | 10 #include "gin/object_template_builder.h" |
| 11 #include "mojo/public/gles2/gles2.h" | 11 #include "mojo/public/gles2/gles2.h" |
| 12 | 12 |
| 13 namespace mojo { | 13 namespace mojo { |
| 14 namespace js { | 14 namespace js { |
| 15 namespace gl { | 15 namespace gl { |
| 16 | 16 |
| 17 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin }; | 17 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin }; |
| 18 | 18 |
| 19 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, | 19 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, |
| 20 int width, int height) { | 20 int width, int height) { |
| 21 return gin::CreateHandle(isolate, new Context(encoded, width, height)); | 21 return gin::CreateHandle(isolate, new Context(encoded, width, height)); |
| 22 } | 22 } |
| 23 | 23 |
| 24 v8::Local<v8::ObjectTemplate> Context::GetObjectTemplate( | |
| 25 v8::Isolate* isolate) { | |
| 26 return gin::ObjectTemplateBuilder(isolate) | |
| 27 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) | |
| 28 .SetMethod("createShader", CreateShader) | |
| 29 .SetMethod("shaderSource", ShaderSource) | |
| 30 .SetMethod("compileShader", CompileShader) | |
| 31 .Build(); | |
| 32 } | |
| 33 | |
| 34 gin::Handle<Shader> Context::CreateShader(const gin::Arguments& args, | 24 gin::Handle<Shader> Context::CreateShader(const gin::Arguments& args, |
| 35 GLenum type) { | 25 GLenum type) { |
| 36 gin::Handle<Shader> result; | 26 gin::Handle<Shader> result; |
| 37 GLuint glshader = glCreateShader(type); | 27 GLuint glshader = glCreateShader(type); |
| 38 if (glshader != 0u) { | 28 if (glshader != 0u) { |
| 39 result = Opaque::Create(args.isolate(), glshader); | 29 result = Opaque::Create(args.isolate(), glshader); |
| 40 } | 30 } |
| 41 return result; | 31 return result; |
| 42 } | 32 } |
| 43 | 33 |
| 44 void Context::ShaderSource(gin::Handle<Shader> shader, | 34 void Context::ShaderSource(gin::Handle<Shader> shader, |
| 45 const std::string& source) { | 35 const std::string& source) { |
| 46 const char* source_chars = source.c_str(); | 36 const char* source_chars = source.c_str(); |
| 47 glShaderSource(shader->value(), 1, &source_chars, NULL); | 37 glShaderSource(shader->value(), 1, &source_chars, NULL); |
| 48 } | 38 } |
| 49 | 39 |
| 50 void Context::CompileShader(const gin::Arguments& args, | 40 void Context::CompileShader(const gin::Arguments& args, |
| 51 gin::Handle<Shader> shader) { | 41 gin::Handle<Shader> shader) { |
| 52 glCompileShader(shader->value()); | 42 glCompileShader(shader->value()); |
| 53 GLint compiled = 0; | 43 GLint compiled = 0; |
| 54 glGetShaderiv(shader->value(), GL_COMPILE_STATUS, &compiled); | 44 glGetShaderiv(shader->value(), GL_COMPILE_STATUS, &compiled); |
| 55 if (!compiled) { | 45 if (!compiled) { |
| 56 // Or should |shader| do it when it is destroyed? | 46 // Or should |shader| do it when it is destroyed? |
| 57 glDeleteShader(shader->value()); | 47 glDeleteShader(shader->value()); |
| 58 args.ThrowTypeError("Could not compile shader"); | 48 args.ThrowTypeError("Could not compile shader"); |
| 59 return; | 49 return; |
| 60 } | 50 } |
| 61 } | 51 } |
| 62 | 52 |
| 53 gin::ObjectTemplateBuilder Context::GetObjectTemplateBuilder( |
| 54 v8::Isolate* isolate) { |
| 55 return gin::Wrappable<Context>::GetObjectTemplateBuilder(isolate) |
| 56 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) |
| 57 .SetMethod("createShader", CreateShader) |
| 58 .SetMethod("shaderSource", ShaderSource) |
| 59 .SetMethod("compileShader", CompileShader); |
| 60 } |
| 61 |
| 63 Context::Context(uint64_t encoded, int width, int height) | 62 Context::Context(uint64_t encoded, int width, int height) |
| 64 : encoded_(encoded) { | 63 : encoded_(encoded) { |
| 65 // TODO(aa): When we want to support multiple contexts, we should add | 64 // TODO(aa): When we want to support multiple contexts, we should add |
| 66 // Context::MakeCurrent() for developers to switch between them. | 65 // Context::MakeCurrent() for developers to switch between them. |
| 67 MojoGLES2MakeCurrent(encoded_); | 66 MojoGLES2MakeCurrent(encoded_); |
| 68 } | 67 } |
| 69 | 68 |
| 70 } // namespace gl | 69 } // namespace gl |
| 71 } // namespace js | 70 } // namespace js |
| 72 } // namespace mojo | 71 } // namespace mojo |
| OLD | NEW |