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/renderer/gpu/compositor_output_surface_software_gl_adapter.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "third_party/skia/include/core/SkPixelRef.h" |
| 10 #include "third_party/skia/include/core/SkDevice.h" |
| 11 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" |
| 12 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
| 13 |
| 14 #include <GLES2/gl2.h> |
| 15 |
| 16 using WebKit::WebImage; |
| 17 using WebKit::WebGraphicsContext3D; |
| 18 using WebKit::WebSize; |
| 19 |
| 20 //------------------------------------------------------------------------------ |
| 21 |
| 22 CompositorOutputSurfaceSoftwareGLAdapter:: |
| 23 CompositorOutputSurfaceSoftwareGLAdapter(WebGraphicsContext3D* context3D) |
| 24 : initialized_(false), |
| 25 program_(0), |
| 26 vertex_shader_(0), |
| 27 fragment_shader_(0), |
| 28 vertex_buffer_(0), |
| 29 framebuffer_texture_id_(0), |
| 30 context3d_(context3D) { |
| 31 } |
| 32 |
| 33 CompositorOutputSurfaceSoftwareGLAdapter:: |
| 34 ~CompositorOutputSurfaceSoftwareGLAdapter() { |
| 35 Destroy(); |
| 36 } |
| 37 |
| 38 WebImage* CompositorOutputSurfaceSoftwareGLAdapter::lock() { |
| 39 image_ = device_->accessBitmap(true); |
| 40 return &image_; |
| 41 } |
| 42 |
| 43 void CompositorOutputSurfaceSoftwareGLAdapter::unlockAndOutputFrame() { |
| 44 Draw(device_->accessBitmap(false).pixelRef()->pixels()); |
| 45 image_.reset(); |
| 46 } |
| 47 |
| 48 void CompositorOutputSurfaceSoftwareGLAdapter::viewportChanged( |
| 49 const WebSize& size) { |
| 50 if (!initialized_) |
| 51 Initialize(); |
| 52 |
| 53 if (framebuffer_texture_size_ != gfx::Size(size)) |
| 54 Resize(size); |
| 55 } |
| 56 |
| 57 void CompositorOutputSurfaceSoftwareGLAdapter::Initialize() { |
| 58 static const char g_vertex_shader[] = |
| 59 "attribute vec4 a_Position;" |
| 60 "attribute vec2 a_texCoord;" |
| 61 "varying vec2 v_texCoord;" |
| 62 "void main() {" |
| 63 " gl_Position = a_Position;" |
| 64 " v_texCoord = a_texCoord;" |
| 65 "}"; |
| 66 |
| 67 // Minimal texture mapping pixel shader, swizzling appropriately. |
| 68 static const char g_fragment_shader[] = |
| 69 "precision mediump float;" |
| 70 "varying vec2 v_texCoord;" |
| 71 "uniform sampler2D s_texture;" |
| 72 "void main() {" |
| 73 " gl_FragColor = texture2D(s_texture, v_texCoord).bgra;" |
| 74 "}"; |
| 75 |
| 76 const GLfloat attribs[] = { |
| 77 -1.0f, -1.0f, |
| 78 1.0f, -1.0f, |
| 79 -1.0f, 1.0f, |
| 80 1.0f, 1.0f, |
| 81 0.0f, 0.0f, |
| 82 1.0f, 0.0f, |
| 83 0.0f, 1.0f, |
| 84 1.0f, 1.0f |
| 85 }; |
| 86 |
| 87 context3d_->makeContextCurrent(); |
| 88 |
| 89 vertex_shader_ = context3d_->createShader(GL_VERTEX_SHADER); |
| 90 context3d_->shaderSource(vertex_shader_, g_vertex_shader); |
| 91 context3d_->compileShader(vertex_shader_); |
| 92 |
| 93 fragment_shader_ = context3d_->createShader(GL_FRAGMENT_SHADER); |
| 94 context3d_->shaderSource(fragment_shader_, g_fragment_shader); |
| 95 context3d_->compileShader(fragment_shader_); |
| 96 |
| 97 program_ = context3d_->createProgram(); |
| 98 context3d_->attachShader(program_, vertex_shader_); |
| 99 context3d_->attachShader(program_, fragment_shader_); |
| 100 |
| 101 vertex_buffer_ = context3d_->createBuffer(); |
| 102 context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 103 context3d_->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), attribs, |
| 104 GL_STATIC_DRAW); |
| 105 context3d_->enableVertexAttribArray(0); |
| 106 context3d_->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 107 context3d_->bindAttribLocation(program_, 0, "a_Position"); |
| 108 context3d_->enableVertexAttribArray(1); |
| 109 context3d_->vertexAttribPointer( |
| 110 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); |
| 111 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); |
| 112 |
| 113 context3d_->linkProgram(program_); |
| 114 context3d_->useProgram(program_); |
| 115 |
| 116 int texture_uniform = context3d_->getUniformLocation(program_, "s_texture"); |
| 117 context3d_->uniform1i(texture_uniform, 0); |
| 118 context3d_->disable(GL_SCISSOR_TEST); |
| 119 context3d_->clearColor(0, 0, 1, 1); |
| 120 |
| 121 initialized_ = true; |
| 122 } |
| 123 |
| 124 void CompositorOutputSurfaceSoftwareGLAdapter::Destroy() { |
| 125 if (!initialized_) |
| 126 return; |
| 127 |
| 128 context3d_->makeContextCurrent(); |
| 129 context3d_->deleteShader(vertex_shader_); |
| 130 context3d_->deleteShader(fragment_shader_); |
| 131 context3d_->deleteProgram(program_); |
| 132 context3d_->deleteBuffer(vertex_buffer_); |
| 133 if (framebuffer_texture_id_) |
| 134 context3d_->deleteTexture(framebuffer_texture_id_); |
| 135 } |
| 136 |
| 137 void CompositorOutputSurfaceSoftwareGLAdapter::Resize( |
| 138 const gfx::Size& viewport_size) { |
| 139 framebuffer_texture_size_ = viewport_size; |
| 140 device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, |
| 141 viewport_size.width(), viewport_size.height(), true)); |
| 142 |
| 143 context3d_->makeContextCurrent(); |
| 144 context3d_->ensureFramebufferCHROMIUM(); |
| 145 framebuffer_texture_id_ = context3d_->createTexture(); |
| 146 context3d_->bindTexture(GL_TEXTURE_2D, framebuffer_texture_id_); |
| 147 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 148 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 149 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 150 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 151 |
| 152 context3d_->viewport(0, 0, viewport_size.width(), viewport_size.height()); |
| 153 context3d_->reshape(viewport_size.width(), viewport_size.height()); |
| 154 } |
| 155 |
| 156 void CompositorOutputSurfaceSoftwareGLAdapter::Draw(void* pixels) { |
| 157 if (!initialized_) |
| 158 NOTREACHED(); |
| 159 if (!framebuffer_texture_id_) |
| 160 NOTREACHED(); |
| 161 |
| 162 context3d_->makeContextCurrent(); |
| 163 context3d_->ensureFramebufferCHROMIUM(); |
| 164 context3d_->clear(GL_COLOR_BUFFER_BIT); |
| 165 |
| 166 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 167 framebuffer_texture_size_.width(), framebuffer_texture_size_.height(), |
| 168 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 169 |
| 170 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 171 |
| 172 context3d_->prepareTexture(); |
| 173 } |
OLD | NEW |