Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: mojo/apps/js/bindings/gl/context.cc

Issue 114883003: Implement more of the JavaScript GL API. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: remove unneeded log Created 7 years ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
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<GLboolean> {
17 static bool FromV8(v8::Isolate* isolate, v8::Handle<v8::Value> val,
18 GLboolean* out) {
19 bool bool_val = false;
20 if (!Converter<bool>::FromV8(isolate, val, &bool_val))
21 return false;
22 *out = static_cast<GLboolean>(bool_val);
23 return true;
24 }
25 };
26 }
27
13 namespace mojo { 28 namespace mojo {
14 namespace js { 29 namespace js {
15 namespace gl { 30 namespace gl {
16 31
17 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin }; 32 gin::WrapperInfo Context::kWrapperInfo = { gin::kEmbedderNativeGin };
18 33
19 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded, 34 gin::Handle<Context> Context::Create(v8::Isolate* isolate, uint64_t encoded,
20 int width, int height) { 35 int width, int height) {
21 return gin::CreateHandle(isolate, new Context(encoded, width, height)); 36 return gin::CreateHandle(isolate, new Context(encoded, width, height));
22 } 37 }
23 38
24 gin::Handle<Shader> Context::CreateShader(const gin::Arguments& args, 39 void Context::BufferData(GLenum target, const gin::ArrayBufferView& buffer,
25 GLenum type) { 40 GLenum usage) {
26 gin::Handle<Shader> result; 41 glBufferData(target, buffer.num_bytes(), buffer.bytes(), usage);
27 GLuint glshader = glCreateShader(type); 42 }
28 if (glshader != 0u) { 43
29 result = Opaque::Create(args.isolate(), glshader); 44 void Context::CompileShader(const gin::Arguments& args, GLuint shader) {
45 glCompileShader(shader);
46 GLint compiled = 0;
47 glGetShaderiv(shader, GL_COMPILE_STATUS, &compiled);
48 if (!compiled) {
49 args.ThrowTypeError(std::string("Could not compile shader: ") +
50 GetShaderInfoLog(shader));
30 } 51 }
52 }
53
54 GLuint Context::CreateBuffer() {
55 GLuint result = 0;
56 glGenBuffers(1, &result);
31 return result; 57 return result;
32 } 58 }
33 59
34 void Context::ShaderSource(gin::Handle<Shader> shader, 60 void Context::DrawElements(GLenum mode, GLsizei count, GLenum type,
35 const std::string& source) { 61 const GLintptr indices) {
36 const char* source_chars = source.c_str(); 62 glDrawElements(mode, count, type, reinterpret_cast<void*>(indices));
37 glShaderSource(shader->value(), 1, &source_chars, NULL);
38 } 63 }
39 64
40 void Context::CompileShader(const gin::Arguments& args, 65 GLint Context::GetAttribLocation(GLuint program, const std::string& name) {
41 gin::Handle<Shader> shader) { 66 return glGetAttribLocation(program, name.c_str());
42 glCompileShader(shader->value()); 67 }
43 GLint compiled = 0; 68
44 glGetShaderiv(shader->value(), GL_COMPILE_STATUS, &compiled); 69 std::string Context::GetProgramInfoLog(GLuint program) {
45 if (!compiled) { 70 GLint info_log_length = 0;
46 // Or should |shader| do it when it is destroyed? 71 glGetProgramiv(program, GL_INFO_LOG_LENGTH, &info_log_length);
47 glDeleteShader(shader->value()); 72 std::string info_log(info_log_length, 0);
48 args.ThrowTypeError("Could not compile shader"); 73 glGetProgramInfoLog(program, info_log_length, NULL, &info_log.front());
49 return; 74 return info_log;
50 } 75 }
76
77 std::string Context::GetShaderInfoLog(GLuint shader) {
78 GLint info_log_length = 0;
79 glGetShaderiv(shader, GL_INFO_LOG_LENGTH, &info_log_length);
80 std::string info_log(info_log_length, 0);
81 glGetShaderInfoLog(shader, info_log_length, NULL, &info_log.front());
82 return info_log;
83 }
84
85 GLint Context::GetUniformLocation(GLuint program, const std::string& name) {
86 return glGetUniformLocation(program, name.c_str());
87 }
88
89 void Context::ShaderSource(GLuint shader, const std::string& source) {
90 const char* source_chars = source.c_str();
91 glShaderSource(shader, 1, &source_chars, NULL);
92 }
93
94 void Context::UniformMatrix4fv(GLint location, GLboolean transpose,
95 const gin::ArrayBufferView& buffer) {
96 glUniformMatrix4fv(location, 1, transpose,
97 static_cast<float*>(buffer.bytes()));
98 }
99
100 void Context::VertexAttribPointer(GLuint index, GLint size, GLenum type,
101 GLboolean normalized, GLsizei stride,
102 GLintptr offset) {
103 // This looks scary, but it's what WebGL does too:
104 // http://www.khronos.org/registry/webgl/specs/latest/1.0/#5.1
105 glVertexAttribPointer(index, size, type, normalized, stride,
106 reinterpret_cast<void*>(offset));
51 } 107 }
52 108
53 gin::ObjectTemplateBuilder Context::GetObjectTemplateBuilder( 109 gin::ObjectTemplateBuilder Context::GetObjectTemplateBuilder(
54 v8::Isolate* isolate) { 110 v8::Isolate* isolate) {
55 return gin::Wrappable<Context>::GetObjectTemplateBuilder(isolate) 111 return gin::ObjectTemplateBuilder(isolate)
112 .SetValue("ARRAY_BUFFER", GL_ARRAY_BUFFER)
113 .SetValue("COLOR_BUFFER_BIT", GL_COLOR_BUFFER_BIT)
114 .SetValue("ELEMENT_ARRAY_BUFFER", GL_ELEMENT_ARRAY_BUFFER)
115 .SetValue("FLOAT", GL_FLOAT)
116 .SetValue("FRAGMENT_SHADER", GL_FRAGMENT_SHADER)
117 .SetValue("STATIC_DRAW", GL_STATIC_DRAW)
118 .SetValue("TRIANGLES", GL_TRIANGLES)
119 .SetValue("UNSIGNED_SHORT", GL_UNSIGNED_SHORT)
56 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER) 120 .SetValue("VERTEX_SHADER", GL_VERTEX_SHADER)
57 .SetMethod("createShader", CreateShader) 121 .SetMethod("attachShader", glAttachShader)
122 .SetMethod("bindBuffer", glBindBuffer)
123 .SetMethod("bufferData", BufferData)
124 .SetMethod("clear", glClear)
125 .SetMethod("clearColor", glClearColor)
126 .SetMethod("compileShader", CompileShader)
127 .SetMethod("createBuffer", CreateBuffer)
128 .SetMethod("createProgram", glCreateProgram)
129 .SetMethod("createShader", glCreateShader)
130 .SetMethod("deleteShader", glDeleteShader)
131 .SetMethod("drawElements", DrawElements)
132 .SetMethod("enableVertexAttribArray", glEnableVertexAttribArray)
133 .SetMethod("getAttribLocation", GetAttribLocation)
134 .SetMethod("getProgramInfoLog", GetProgramInfoLog)
135 .SetMethod("getShaderInfoLog", GetShaderInfoLog)
136 .SetMethod("getUniformLocation", GetUniformLocation)
137 .SetMethod("linkProgram", glLinkProgram)
58 .SetMethod("shaderSource", ShaderSource) 138 .SetMethod("shaderSource", ShaderSource)
59 .SetMethod("compileShader", CompileShader); 139 .SetMethod("swapBuffers", MojoGLES2SwapBuffers)
140 .SetMethod("uniformMatrix4fv", UniformMatrix4fv)
141 .SetMethod("useProgram", glUseProgram)
142 .SetMethod("vertexAttribPointer", VertexAttribPointer)
143 .SetMethod("viewport", glViewport);
60 } 144 }
61 145
62 Context::Context(uint64_t encoded, int width, int height) 146 Context::Context(uint64_t encoded, int width, int height)
63 : encoded_(encoded) { 147 : encoded_(encoded) {
64 // TODO(aa): When we want to support multiple contexts, we should add 148 // TODO(aa): When we want to support multiple contexts, we should add
65 // Context::MakeCurrent() for developers to switch between them. 149 // Context::MakeCurrent() for developers to switch between them.
66 MojoGLES2MakeCurrent(encoded_); 150 MojoGLES2MakeCurrent(encoded_);
67 } 151 }
68 152
69 } // namespace gl 153 } // namespace gl
70 } // namespace js 154 } // namespace js
71 } // namespace mojo 155 } // namespace mojo
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698