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/array_buffer.h" | |
10 #include "gin/object_template_builder.h" | 11 #include "gin/object_template_builder.h" |
11 #include "mojo/public/gles2/gles2.h" | 12 #include "mojo/public/gles2/gles2.h" |
12 | 13 |
14 namespace gin { | |
15 template<> | |
16 struct Converter<GLvoid*> { | |
17 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
18 GLvoid** out) { | |
19 int64_t int_val = 0; | |
20 if (!Converter<int64_t>::FromV8(isolate, val, &int_val)) | |
21 return false; | |
22 *out = reinterpret_cast<GLvoid*>(int_val); | |
abarth-chromium
2013/12/18 01:19:30
Woah. Where is this used? I would have expected
Aaron Boodman
2013/12/18 02:19:08
It's used by glVertexAttribPointer: http://www.ope
| |
23 return true; | |
24 } | |
25 }; | |
26 | |
27 template<> | |
28 struct Converter<GLboolean> { | |
29 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val, | |
30 GLboolean* out) { | |
31 bool bool_val = false; | |
32 if (!Converter<bool>::FromV8(isolate, val, &bool_val)) | |
33 return false; | |
34 *out = static_cast<GLboolean>(bool_val); | |
35 return true; | |
36 } | |
37 }; | |
38 } | |
39 | |
13 namespace mojo { | 40 namespace mojo { |
14 namespace js { | 41 namespace js { |
15 namespace gl { | 42 namespace gl { |
16 | 43 |
17 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin }; | 44 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin }; |
18 | 45 |
19 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, | 46 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, |
20 int width, int height) { | 47 int width, int height) { |
21 return gin::CreateHandle(isolate, new Context(encoded, width, height)); | 48 return gin::CreateHandle(isolate, new Context(encoded, width, height)); |
22 } | 49 } |
23 | 50 |
24 v8::Local<v8::ObjectTemplate> Context::GetObjectTemplate( | 51 v8::Local<v8::ObjectTemplate> Context::GetObjectTemplate( |
25 v8::Isolate* isolate) { | 52 v8::Isolate* isolate) { |
26 return gin::ObjectTemplateBuilder(isolate) | 53 return gin::ObjectTemplateBuilder(isolate) |
54 .SetValue("ARRAY_BUFFER", GL_ARRAY_BUFFER) | |
55 .SetValue("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT) | |
56 .SetValue("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER) | |
57 .SetValue("FLOAT", GL_FLOAT) | |
58 .SetValue("FRAGMENT_SHADER", GL_FRAGMENT_SHADER) | |
59 .SetValue("STATIC_DRAW", GL_STATIC_DRAW) | |
60 .SetValue("TRIANGLES", GL_TRIANGLES) | |
61 .SetValue("UNSIGNED_SHORT", GL_UNSIGNED_SHORT) | |
27 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) | 62 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) |
28 .SetMethod("createShader", CreateShader) | 63 .SetMethod("attachShader", glAttachShader) |
64 .SetMethod("bindBuffer", glBindBuffer) | |
65 .SetMethod("bufferData", BufferData) | |
66 .SetMethod("clear", glClear) | |
67 .SetMethod("clearColor", glClearColor) | |
68 .SetMethod("compileShader", CompileShader) | |
69 .SetMethod("createBuffer", CreateBuffer) | |
70 .SetMethod("createProgram", glCreateProgram) | |
71 .SetMethod("createShader", glCreateShader) | |
72 .SetMethod("deleteShader", glDeleteShader) | |
73 .SetMethod("drawElements", glDrawElements) | |
74 .SetMethod("enableVertexAttribArray", glEnableVertexAttribArray) | |
75 .SetMethod("getAttribLocation", GetAttribLocation) | |
76 .SetMethod("getProgramInfoLog", GetProgramInfoLog) | |
77 .SetMethod("getShaderInfoLog", GetShaderInfoLog) | |
78 .SetMethod("getUniformLocation", GetUniformLocation) | |
79 .SetMethod("linkProgram", glLinkProgram) | |
29 .SetMethod("shaderSource", ShaderSource) | 80 .SetMethod("shaderSource", ShaderSource) |
30 .SetMethod("compileShader", CompileShader) | 81 .SetMethod("swapBuffers", MojoGLES2SwapBuffers) |
82 .SetMethod("uniformMatrix4fv", UniformMatrix4fv) | |
83 .SetMethod("useProgram", glUseProgram) | |
84 .SetMethod("vertexAttribPointer", glVertexAttribPointer) | |
85 .SetMethod("viewport", glViewport) | |
31 .Build(); | 86 .Build(); |
32 } | 87 } |
33 | 88 |
34 gin::Handle<Shader> Context::CreateShader(const gin::Arguments& args, | 89 void Context::BufferData(GLenum target, const gin::ArrayBufferView& buffer, |
35 GLenum type) { | 90 GLenum usage) { |
36 gin::Handle<Shader> result; | 91 glBufferData(target, buffer.num_bytes(), buffer.bytes(), usage); |
37 GLuint glshader = glCreateShader(type); | 92 } |
38 if (glshader != 0u) { | 93 |
39 result = Opaque::Create(args.isolate(), glshader); | 94 void Context::CompileShader(const gin::Arguments& args, GLuint shader) { |
95 glCompileShader(shader); | |
96 GLint compiled = 0; | |
97 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled); | |
98 if (!compiled) { | |
99 args.ThrowTypeError(std::string("Could not compile shader: ") + | |
100 GetShaderInfoLog(shader)); | |
40 } | 101 } |
102 } | |
103 | |
104 GLuint Context::CreateBuffer() { | |
105 GLuint result = 0; | |
106 glGenBuffers(1, &result); | |
41 return result; | 107 return result; |
42 } | 108 } |
43 | 109 |
44 void Context::ShaderSource(gin::Handle<Shader> shader, | 110 GLint Context::GetAttribLocation(GLuint program, const std::string& name) { |
45 const std::string& source) { | 111 return glGetAttribLocation(program, name.c_str()); |
46 const char* source_chars = source.c_str(); | |
47 glShaderSource(shader->value(), 1, &source_chars, NULL); | |
48 } | 112 } |
49 | 113 |
50 void Context::CompileShader(const gin::Arguments& args, | 114 std::string Context::GetProgramInfoLog(GLuint program) { |
51 gin::Handle<Shader> shader) { | 115 GLint info_log_length = 0; |
52 glCompileShader(shader->value()); | 116 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length); |
53 GLint compiled = 0; | 117 std::string info_log(info_log_length, 0); |
54 glGetShaderiv(shader->value(), GL_COMPILE_STATUS, &compiled); | 118 glGetProgramInfoLog(program, info_log_length, NULL, &info_log.front()); |
55 if (!compiled) { | 119 return info_log; |
56 // Or should |shader| do it when it is destroyed? | 120 } |
57 glDeleteShader(shader->value()); | 121 |
58 args.ThrowTypeError("Could not compile shader"); | 122 std::string Context::GetShaderInfoLog(GLuint shader) { |
59 return; | 123 GLint info_log_length = 0; |
60 } | 124 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length); |
125 std::string info_log(info_log_length, 0); | |
126 glGetShaderInfoLog(shader, info_log_length, NULL, &info_log.front()); | |
127 return info_log; | |
128 } | |
129 | |
130 GLint Context::GetUniformLocation(GLuint program, const std::string& name) { | |
131 return glGetUniformLocation(program, name.c_str()); | |
132 } | |
133 | |
134 void Context::ShaderSource(GLuint shader, const std::string& source) { | |
135 const char* source_chars = source.c_str(); | |
136 glShaderSource(shader, 1, &source_chars, NULL); | |
137 } | |
138 | |
139 void Context::UniformMatrix4fv(GLint location, GLboolean transpose, | |
140 const gin::ArrayBufferView& buffer) { | |
141 glUniformMatrix4fv(location, 1, transpose, | |
142 static_cast<float*>(buffer.bytes())); | |
61 } | 143 } |
62 | 144 |
63 Context::Context(uint64_t encoded, int width, int height) | 145 Context::Context(uint64_t encoded, int width, int height) |
64 : encoded_(encoded) { | 146 : encoded_(encoded) { |
65 // TODO(aa): When we want to support multiple contexts, we should add | 147 // TODO(aa): When we want to support multiple contexts, we should add |
66 // Context::MakeCurrent() for developers to switch between them. | 148 // Context::MakeCurrent() for developers to switch between them. |
67 MojoGLES2MakeCurrent(encoded_); | 149 MojoGLES2MakeCurrent(encoded_); |
68 } | 150 } |
69 | 151 |
70 } // namespace gl | 152 } // namespace gl |
71 } // namespace js | 153 } // namespace js |
72 } // namespace mojo | 154 } // namespace mojo |
OLD | NEW |