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_software_output_device_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 CompositorSoftwareOutputDeviceGLAdapter:: |
| 23 CompositorSoftwareOutputDeviceGLAdapter(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 locked_for_write_(false) { |
| 32 } |
| 33 |
| 34 CompositorSoftwareOutputDeviceGLAdapter:: |
| 35 ~CompositorSoftwareOutputDeviceGLAdapter() { |
| 36 Destroy(); |
| 37 } |
| 38 |
| 39 WebImage* CompositorSoftwareOutputDeviceGLAdapter::lock(bool forWrite) { |
| 40 locked_for_write_ = forWrite; |
| 41 image_ = device_->accessBitmap(forWrite); |
| 42 return &image_; |
| 43 } |
| 44 |
| 45 void CompositorSoftwareOutputDeviceGLAdapter::unlock() { |
| 46 if (locked_for_write_) |
| 47 Draw(device_->accessBitmap(false).pixelRef()->pixels()); |
| 48 image_.reset(); |
| 49 } |
| 50 |
| 51 void CompositorSoftwareOutputDeviceGLAdapter::didChangeViewportSize( |
| 52 WebSize size) { |
| 53 if (!initialized_) |
| 54 Initialize(); |
| 55 |
| 56 if (framebuffer_texture_size_ != gfx::Size(size)) |
| 57 Resize(size); |
| 58 } |
| 59 |
| 60 void CompositorSoftwareOutputDeviceGLAdapter::Initialize() { |
| 61 // Vertex shader that flips the y axis. |
| 62 static const char g_vertex_shader[] = |
| 63 "attribute vec4 a_Position;" |
| 64 "attribute vec2 a_texCoord;" |
| 65 "varying vec2 v_texCoord;" |
| 66 "void main() {" |
| 67 " gl_Position = a_Position;" |
| 68 " gl_Position.y = -gl_Position.y;" |
| 69 " v_texCoord = a_texCoord;" |
| 70 "}"; |
| 71 |
| 72 // Pixel shader that swizzles RGBA -> BGRA. |
| 73 static const char g_fragment_shader[] = |
| 74 "precision mediump float;" |
| 75 "varying vec2 v_texCoord;" |
| 76 "uniform sampler2D s_texture;" |
| 77 "void main() {" |
| 78 " gl_FragColor = texture2D(s_texture, v_texCoord).bgra;" |
| 79 "}"; |
| 80 |
| 81 const GLfloat attribs[] = { |
| 82 -1.0f, -1.0f, |
| 83 1.0f, -1.0f, |
| 84 -1.0f, 1.0f, |
| 85 1.0f, 1.0f, |
| 86 0.0f, 0.0f, |
| 87 1.0f, 0.0f, |
| 88 0.0f, 1.0f, |
| 89 1.0f, 1.0f |
| 90 }; |
| 91 |
| 92 context3d_->makeContextCurrent(); |
| 93 |
| 94 vertex_shader_ = context3d_->createShader(GL_VERTEX_SHADER); |
| 95 context3d_->shaderSource(vertex_shader_, g_vertex_shader); |
| 96 context3d_->compileShader(vertex_shader_); |
| 97 |
| 98 fragment_shader_ = context3d_->createShader(GL_FRAGMENT_SHADER); |
| 99 context3d_->shaderSource(fragment_shader_, g_fragment_shader); |
| 100 context3d_->compileShader(fragment_shader_); |
| 101 |
| 102 program_ = context3d_->createProgram(); |
| 103 context3d_->attachShader(program_, vertex_shader_); |
| 104 context3d_->attachShader(program_, fragment_shader_); |
| 105 |
| 106 vertex_buffer_ = context3d_->createBuffer(); |
| 107 context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
| 108 context3d_->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), attribs, |
| 109 GL_STATIC_DRAW); |
| 110 context3d_->enableVertexAttribArray(0); |
| 111 context3d_->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
| 112 context3d_->bindAttribLocation(program_, 0, "a_Position"); |
| 113 context3d_->enableVertexAttribArray(1); |
| 114 context3d_->vertexAttribPointer( |
| 115 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); |
| 116 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); |
| 117 |
| 118 context3d_->linkProgram(program_); |
| 119 context3d_->useProgram(program_); |
| 120 |
| 121 int texture_uniform = context3d_->getUniformLocation(program_, "s_texture"); |
| 122 context3d_->uniform1i(texture_uniform, 0); |
| 123 context3d_->disable(GL_SCISSOR_TEST); |
| 124 context3d_->clearColor(0, 0, 1, 1); |
| 125 |
| 126 initialized_ = true; |
| 127 } |
| 128 |
| 129 void CompositorSoftwareOutputDeviceGLAdapter::Destroy() { |
| 130 if (!initialized_) |
| 131 return; |
| 132 |
| 133 context3d_->makeContextCurrent(); |
| 134 context3d_->deleteShader(vertex_shader_); |
| 135 context3d_->deleteShader(fragment_shader_); |
| 136 context3d_->deleteProgram(program_); |
| 137 context3d_->deleteBuffer(vertex_buffer_); |
| 138 if (framebuffer_texture_id_) |
| 139 context3d_->deleteTexture(framebuffer_texture_id_); |
| 140 } |
| 141 |
| 142 void CompositorSoftwareOutputDeviceGLAdapter::Resize( |
| 143 const gfx::Size& viewport_size) { |
| 144 framebuffer_texture_size_ = viewport_size; |
| 145 device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, |
| 146 viewport_size.width(), viewport_size.height(), true)); |
| 147 |
| 148 context3d_->makeContextCurrent(); |
| 149 context3d_->ensureFramebufferCHROMIUM(); |
| 150 framebuffer_texture_id_ = context3d_->createTexture(); |
| 151 context3d_->bindTexture(GL_TEXTURE_2D, framebuffer_texture_id_); |
| 152 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
| 153 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
| 154 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
| 155 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
| 156 |
| 157 context3d_->viewport(0, 0, viewport_size.width(), viewport_size.height()); |
| 158 context3d_->reshape(viewport_size.width(), viewport_size.height()); |
| 159 } |
| 160 |
| 161 void CompositorSoftwareOutputDeviceGLAdapter::Draw(void* pixels) { |
| 162 if (!initialized_) |
| 163 NOTREACHED(); |
| 164 if (!framebuffer_texture_id_) |
| 165 NOTREACHED(); |
| 166 |
| 167 context3d_->makeContextCurrent(); |
| 168 context3d_->ensureFramebufferCHROMIUM(); |
| 169 context3d_->clear(GL_COLOR_BUFFER_BIT); |
| 170 |
| 171 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 172 framebuffer_texture_size_.width(), framebuffer_texture_size_.height(), |
| 173 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
| 174 |
| 175 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 176 |
| 177 context3d_->prepareTexture(); |
| 178 } |
OLD | NEW |