|
| 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/WebGraphicsContext3D.h" |
| 10 | + |
| 11 | +namespace { |
| 12 | + |
| 13 | +static const char g_vertex_shader[] = |
| 14 | + "attribute vec4 a_Position;" |
| 15 | + "attribute vec2 a_texCoord;" |
| 16 | + "varying vec2 v_texCoord;" |
| 17 | + "void main() {" |
| 18 | + " gl_Position = a_Position;" |
| 19 | + " v_texCoord = a_texCoord;" |
| 20 | + "}"; |
| 21 | + |
| 22 | +// Minimal texture mapping pixel shader. |
| 23 | +static const char g_fragment_shader[] = |
| 24 | + "precision mediump float;" |
| 25 | + "varying vec2 v_texCoord;" |
| 26 | + "uniform sampler2D s_texture;" |
| 27 | + "void main() {" |
| 28 | + " gl_FragColor = texture2D(s_texture, v_texCoord);" |
| 29 | + "}"; |
| 30 | + |
| 31 | +} // anonymous namespace |
| 32 | + |
| 33 | +namespace content { |
| 34 | + |
| 35 | +DrawContext::DrawContext(ANativeWindow* window) |
| 36 | + : program_(GL_ZERO), |
| 37 | + vertex_shader_(0), |
| 38 | + fragment_shader_(0), |
| 39 | + texture_uniform_(GL_ZERO), |
| 40 | + vertex_buffer_(GL_ZERO), |
| 41 | + context_(content::GraphicsContext::CreateForUI(window)) { |
| 42 | + const GLfloat attribs[] = { |
| 43 | + -1.0f, -1.0f, |
| 44 | + 1.0f, -1.0f, |
| 45 | + -1.0f, 1.0f, |
| 46 | + 1.0f, 1.0f, |
| 47 | + 0.0f, 0.0f, |
| 48 | + 1.0f, 0.0f, |
| 49 | + 0.0f, 1.0f, |
| 50 | + 1.0f, 1.0f }; |
| 51 | + WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); |
| 52 | + vertex_buffer_ = context3D->createBuffer(); |
| 53 | + context3D->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 54 | + context3D->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(GLfloat), attribs, |
| 55 | + GL_STATIC_DRAW); |
| 56 | + |
| 57 | + vertex_shader_ = context3D->createShader(GL_VERTEX_SHADER); |
| 58 | + context3D->shaderSource(vertex_shader_, g_vertex_shader); |
| 59 | + context3D->compileShader(vertex_shader_); |
| 60 | + |
| 61 | + fragment_shader_ = context3D->createShader(GL_FRAGMENT_SHADER); |
| 62 | + context3D->shaderSource(fragment_shader_, g_fragment_shader); |
| 63 | + context3D->compileShader(fragment_shader_); |
| 64 | + |
| 65 | + program_ = context3D->createProgram(); |
| 66 | + context3D->attachShader(program_, vertex_shader_); |
| 67 | + context3D->attachShader(program_, fragment_shader_); |
| 68 | + context3D->linkProgram(program_); |
| 69 | + |
| 70 | + texture_uniform_ = context3D->getUniformLocation(program_, "s_texture"); |
| 71 | +} |
| 72 | + |
| 73 | +DrawContext::~DrawContext() { |
| 74 | + if (vertex_buffer_ != GL_ZERO) |
| 75 | + context_->GetContext3D()->deleteBuffer(vertex_buffer_); |
| 76 | + if (program_ != GL_ZERO) |
| 77 | + context_->GetContext3D()->deleteProgram(program_); |
| 78 | +} |
| 79 | + |
| 80 | +uint32 DrawContext::Draw(int texture) { |
| 81 | + DCHECK(program_ != GL_ZERO); |
| 82 | + WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); |
| 83 | + |
| 84 | + context3D->useProgram(program_); |
| 85 | + |
| 86 | + context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 87 | + context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 88 | + context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 89 | + context3D->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 90 | + |
| 91 | + context3D->uniform1i(texture_uniform_, 0); |
| 92 | + context3D->activeTexture(GL_TEXTURE0); |
| 93 | + context3D->bindTexture(GL_TEXTURE_2D, texture); |
| 94 | + |
| 95 | + context3D->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 96 | + context3D->enableVertexAttribArray(0); |
| 97 | + context3D->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 98 | + context3D->bindAttribLocation(program_, 0, "a_Position"); |
| 99 | + context3D->enableVertexAttribArray(1); |
| 100 | + context3D->vertexAttribPointer( |
| 101 | + 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); |
| 102 | + context3D->bindAttribLocation(program_, 1, "a_texCoord"); |
| 103 | + |
| 104 | + context3D->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 105 | + |
| 106 | + context3D->prepareTexture(); |
| 107 | + |
| 108 | + return context_->InsertSyncPoint(); |
| 109 | +} |
| 110 | + |
| 111 | +void DrawContext::Reshape(int width, int height) { |
| 112 | + WebKit::WebGraphicsContext3D* context3D = context_->GetContext3D(); |
| 113 | + context3D->viewport(0, 0, width, height); |
| 114 | + context3D->reshape(width, height); |
| 115 | +} |
| 116 | + |
| 117 | +} // namespace content |
0 commit comments