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