Index: content/renderer/gpu/compositor_output_canvas_3d_to_2d_adapter.cc |
diff --git a/content/renderer/gpu/compositor_output_canvas_3d_to_2d_adapter.cc b/content/renderer/gpu/compositor_output_canvas_3d_to_2d_adapter.cc |
new file mode 100644 |
index 0000000000000000000000000000000000000000..d8a0d4a7dc31e53682d2114c60186030976d6db5 |
--- /dev/null |
+++ b/content/renderer/gpu/compositor_output_canvas_3d_to_2d_adapter.cc |
@@ -0,0 +1,157 @@ |
+// Copyright (c) 2012 The Chromium Authors. All rights reserved. |
+// Use of this source code is governed by a BSD-style license that can be |
+// found in the LICENSE file. |
+ |
+#include "content/renderer/gpu/compositor_output_canvas_3d_to_2d_adapter.h" |
+ |
+#include "base/logging.h" |
+#include "third_party/skia/include/core/SkBitmap.h" |
+#include "third_party/skia/include/core/SkPixelRef.h" |
+#include "third_party/skia/include/core/SkDevice.h" |
+#include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3D.h" |
+#include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" |
+ |
+#include <GLES2/gl2.h> |
+ |
+using WebKit::WebImage; |
+using WebKit::WebGraphicsContext3D; |
+using WebKit::WebSize; |
+ |
+//------------------------------------------------------------------------------ |
+ |
+CompositorOutputCanvas3DTo2DAdapter::CompositorOutputCanvas3DTo2DAdapter( |
+ WebGraphicsContext3D* context3D) |
+ : initialized_(false) |
+ , context3d_(context3D) { |
piman
2012/08/28 03:31:57
nit: style
CompositorOutputCanvas3DTo2DAdapter::C
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Done.
|
+} |
+ |
+CompositorOutputCanvas3DTo2DAdapter::~CompositorOutputCanvas3DTo2DAdapter() { |
+} |
+ |
+WebImage* CompositorOutputCanvas3DTo2DAdapter::lock() { |
+ if (!initialized_) |
piman
2012/08/28 03:31:57
you could also make Initialize() explicit when the
aelias_OOO_until_Jul13
2012/08/28 23:10:58
I'd rather not introduce a new API. But I moved t
piman
2012/08/28 23:26:59
I was really thinking, CompositorOutputSurface can
aelias_OOO_until_Jul13
2012/08/28 23:53:11
I'd also like CompositorOutputSurface to stay indi
|
+ initGLAdapter(); |
+ |
+ image_ = device_->accessBitmap(true); |
+ return &image_; |
+} |
+ |
+void CompositorOutputCanvas3DTo2DAdapter::unlockAndOutputFrame() { |
+ frameDrawGLAdapter(device_->accessBitmap(false).pixelRef()->pixels()); |
+ image_.reset(); |
+} |
+ |
+void CompositorOutputCanvas3DTo2DAdapter::viewportChanged(const WebSize& size) { |
+ if (framebufferTextureSize_ != gfx::Size(size)) |
+ textureGLAdapter(size); |
+} |
+ |
+void CompositorOutputCanvas3DTo2DAdapter::initGLAdapter() { |
+ static const char g_vertex_shader[] = |
+ "attribute vec4 a_Position;" |
+ "attribute vec2 a_texCoord;" |
+ "varying vec2 v_texCoord;" |
+ "void main() {" |
+ " gl_Position = a_Position;" |
+ " v_texCoord = a_texCoord;" |
+ "}"; |
+ |
+ // Minimal texture mapping pixel shader, swizzling appropriately. |
+ static const char g_fragment_shader[] = |
+ "precision mediump float;" |
+ "varying vec2 v_texCoord;" |
+ "uniform sampler2D s_texture;" |
+ "void main() {" |
+ " gl_FragColor = texture2D(s_texture, v_texCoord).bgra;" |
+ "}"; |
+ |
+ const GLfloat attribs[] = { |
+ -1.0f, -1.0f, |
+ 1.0f, -1.0f, |
+ -1.0f, 1.0f, |
+ 1.0f, 1.0f, |
+ 0.0f, 0.0f, |
+ 1.0f, 0.0f, |
+ 0.0f, 1.0f, |
+ 1.0f, 1.0f }; |
+ |
+ context3d_->makeContextCurrent(); |
+ |
+ vertex_buffer_ = context3d_->createBuffer(); |
piman
2012/08/28 03:31:57
We will need to release all resources: VBOs, shade
aelias_OOO_until_Jul13
2012/08/28 23:10:58
I added some cleanup stuff in the destructor. But
piman
2012/08/28 23:26:59
Most of the time, if we only have one tab in the r
|
+ context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
+ context3d_->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), attribs, |
+ GL_STATIC_DRAW); |
+ |
+ vertex_shader_ = context3d_->createShader(GL_VERTEX_SHADER); |
+ context3d_->shaderSource(vertex_shader_, g_vertex_shader); |
+ context3d_->compileShader(vertex_shader_); |
+ |
+ fragment_shader_ = context3d_->createShader(GL_FRAGMENT_SHADER); |
+ context3d_->shaderSource(fragment_shader_, g_fragment_shader); |
+ context3d_->compileShader(fragment_shader_); |
+ |
+ program_ = context3d_->createProgram(); |
+ context3d_->attachShader(program_, vertex_shader_); |
+ context3d_->attachShader(program_, fragment_shader_); |
+ context3d_->linkProgram(program_); |
+ |
+ texture_uniform_ = context3d_->getUniformLocation(program_, "s_texture"); |
piman
2012/08/28 03:31:57
you can set all the program state at this point, s
|
+ |
+ initialized_ = true; |
+} |
+ |
+void CompositorOutputCanvas3DTo2DAdapter::textureGLAdapter( |
+ const gfx::Size& viewportSize) { |
+ framebufferTextureSize_ = viewportSize; |
+ device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, |
+ viewportSize.width(), viewportSize.height(), true)); |
+ |
+ context3d_->makeContextCurrent(); |
+ context3d_->ensureFramebufferCHROMIUM(); |
+ framebufferTextureId_ = context3d_->createTexture(); |
+ context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); |
+ context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
+ context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
+ context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
+ context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
+ |
+ context3d_->viewport(0, 0, viewportSize.width(), viewportSize.height()); |
+ context3d_->reshape(viewportSize.width(), viewportSize.height()); |
+} |
+ |
+void CompositorOutputCanvas3DTo2DAdapter::frameDrawGLAdapter(void* pixels) { |
+ if (program_ == GL_ZERO) |
piman
2012/08/28 03:31:57
if (!program_)
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Done.
|
+ NOTREACHED(); |
+ if (framebufferTextureId_ == GL_ZERO) |
piman
2012/08/28 03:31:57
if (!framebuffer_texture_id_)
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Done.
|
+ NOTREACHED(); |
+ |
+ context3d_->makeContextCurrent(); |
+ context3d_->ensureFramebufferCHROMIUM(); |
+ context3d_->clearColor(0, 0, 1, 1); |
+ context3d_->disable(GL_SCISSOR_TEST); |
piman
2012/08/28 03:31:57
move to Initialize()
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Moved it all to Initialize().
|
+ context3d_->clear(GL_COLOR_BUFFER_BIT); |
+ |
+ context3d_->activeTexture(GL_TEXTURE0); |
piman
2012/08/28 03:31:57
move to Initialize() (actually, it's redundant, TE
|
+ context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); |
piman
2012/08/28 03:31:57
redundant
|
+ context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
+ framebufferTextureSize_.width(), framebufferTextureSize_.height(), |
+ 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); |
+ |
+ context3d_->useProgram(program_); |
piman
2012/08/28 03:31:57
move to Initialize()
|
+ context3d_->uniform1i(texture_uniform_, 0); |
piman
2012/08/28 03:31:57
move to Initialize()
|
+ context3d_->activeTexture(GL_TEXTURE0); |
piman
2012/08/28 03:31:57
redundant
|
+ context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); |
piman
2012/08/28 03:31:57
redundant
|
+ |
+ context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); |
+ context3d_->enableVertexAttribArray(0); |
+ context3d_->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); |
+ context3d_->bindAttribLocation(program_, 0, "a_Position"); |
+ context3d_->enableVertexAttribArray(1); |
+ context3d_->vertexAttribPointer( |
+ 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); |
+ context3d_->bindAttribLocation(program_, 1, "a_texCoord"); |
piman
2012/08/28 03:31:57
Just move all the attrib binding to Initialize.
No
|
+ |
+ context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
+ |
+ context3d_->prepareTexture(); |
+} |