Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2012 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 "content/shell/android/draw_context.h" | |
| 6 | |
| 7 #include "base/logging.h" | |
| 8 #include "content/public/browser/android/graphics_context.h" | |
| 9 #include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebGraphicsC ontext3D.h" | |
| 10 | |
| 11 static const char g_vertex_shader[] = | |
| 12 "attribute vec4 a_Position;\n" | |
| 13 "attribute vec2 a_texCoord;\n" | |
| 14 "varying vec2 v_texCoord;\n" | |
| 15 "void main() {\n" | |
| 16 " gl_Position = a_Position;\n" | |
| 17 " v_texCoord = a_texCoord;\n" | |
| 18 "}\n"; | |
| 19 | |
| 20 // Minimal texture mapping pixel shader. | |
| 21 static const char g_fragment_shader[] = | |
| 22 "precision mediump float;" | |
| 23 "varying vec2 v_texCoord;\n" | |
| 24 "uniform sampler2D s_texture;\n" | |
| 25 "void main() {\n" | |
| 26 " gl_FragColor = texture2D(s_texture, v_texCoord);\n" | |
| 27 "}\n"; | |
| 28 | |
| 29 DrawContext::DrawContext(ANativeWindow* window) | |
| 30 : program_(GL_ZERO), | |
| 31 vertex_shader_(0), | |
| 32 fragment_shader_(0), | |
| 33 texture_uniform_(GL_ZERO), | |
| 34 vertex_buffer_(GL_ZERO), | |
| 35 context_(content::GraphicsContext::CreateForUI(window)) { | |
| 36 const GLfloat attribs[] = { | |
| 37 -1.0f, -1.0f, | |
|
Ted C
2012/07/27 20:31:15
increase indent by one or should this be 4 and not
no sievers
2012/07/31 01:28:44
Done.
| |
| 38 1.0f, -1.0f, | |
| 39 -1.0f, 1.0f, | |
| 40 1.0f, 1.0f, | |
| 41 0.0f, 0.0f, | |
| 42 1.0f, 0.0f, | |
| 43 0.0f, 1.0f, | |
| 44 1.0f, 1.0f }; | |
| 45 WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); | |
| 46 vertex_buffer_ = context3D->createBuffer(); | |
| 47 context3D->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 48 context3D->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), attribs, | |
| 49 GL_STATIC_DRAW); | |
|
Ted C
2012/07/27 20:31:15
align with above
no sievers
2012/07/31 01:28:44
Done.
| |
| 50 | |
| 51 | |
| 52 vertex_shader_ = context3D->createShader(GL_VERTEX_SHADER); | |
| 53 context3D->shaderSource(vertex_shader_, g_vertex_shader); | |
| 54 context3D->compileShader(vertex_shader_); | |
| 55 | |
| 56 fragment_shader_ = context3D->createShader(GL_FRAGMENT_SHADER); | |
| 57 context3D->shaderSource(fragment_shader_, g_fragment_shader); | |
| 58 context3D->compileShader(fragment_shader_); | |
| 59 | |
| 60 program_ = context3D->createProgram(); | |
| 61 context3D->attachShader(program_, vertex_shader_); | |
| 62 context3D->attachShader(program_, fragment_shader_); | |
| 63 context3D->linkProgram(program_); | |
| 64 | |
| 65 texture_uniform_ = context3D->getUniformLocation(program_, "s_texture"); | |
| 66 | |
|
Ted C
2012/07/27 20:31:15
remove blank line
no sievers
2012/07/31 01:28:44
Done.
| |
| 67 } | |
| 68 | |
| 69 DrawContext::~DrawContext() { | |
| 70 if (vertex_buffer_ != GL_ZERO) | |
| 71 context_->GetContext3D()->deleteBuffer(vertex_buffer_); | |
| 72 if (program_ != GL_ZERO) | |
| 73 context_->GetContext3D()->deleteProgram(program_); | |
| 74 } | |
| 75 | |
| 76 uint32 DrawContext::Draw(int texture) { | |
| 77 DCHECK(program_ != GL_ZERO); | |
| 78 WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); | |
| 79 | |
| 80 context3D->useProgram(program_); | |
| 81 | |
| 82 context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
| 83 context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
| 84 context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
| 85 context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
| 86 | |
| 87 context3D->uniform1i(texture_uniform_, 0); | |
| 88 context3D->activeTexture(GL_TEXTURE0); | |
| 89 context3D->bindTexture(GL_TEXTURE_2D, texture); | |
| 90 | |
| 91 context3D->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
| 92 context3D->enableVertexAttribArray(0); | |
| 93 context3D->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
| 94 context3D->bindAttribLocation(program_, 0, "a_Position"); | |
| 95 context3D->enableVertexAttribArray(1); | |
| 96 context3D->vertexAttribPointer( | |
| 97 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); | |
| 98 context3D->bindAttribLocation(program_, 1, "a_texCoord"); | |
| 99 | |
| 100 context3D->drawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
| 101 | |
| 102 context3D->prepareTexture(); | |
| 103 | |
| 104 return context_->InsertSyncPoint(); | |
| 105 } | |
| 106 | |
| 107 void DrawContext::Reshape(int width, int height) { | |
| 108 WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); | |
| 109 context3D->viewport(0, 0, width, height); | |
| 110 context3D->reshape(width, height); | |
| 111 } | |
| OLD | NEW |