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_canvas_3d_to_2d_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 CompositorOutputCanvas3DTo2DAdapter::CompositorOutputCanvas3DTo2DAdapter( | |
23 WebGraphicsContext3D* context3D) | |
24 : initialized_(false) | |
25 , context3d_(context3D) { | |
piman
2012/08/28 03:31:57
nit: style
CompositorOutputCanvas3DTo2DAdapter::C
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Done.
| |
26 } | |
27 | |
28 CompositorOutputCanvas3DTo2DAdapter::~CompositorOutputCanvas3DTo2DAdapter() { | |
29 } | |
30 | |
31 WebImage* CompositorOutputCanvas3DTo2DAdapter::lock() { | |
32 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
| |
33 initGLAdapter(); | |
34 | |
35 image_ = device_->accessBitmap(true); | |
36 return &image_; | |
37 } | |
38 | |
39 void CompositorOutputCanvas3DTo2DAdapter::unlockAndOutputFrame() { | |
40 frameDrawGLAdapter(device_->accessBitmap(false).pixelRef()->pixels()); | |
41 image_.reset(); | |
42 } | |
43 | |
44 void CompositorOutputCanvas3DTo2DAdapter::viewportChanged(const WebSize& size) { | |
45 if (framebufferTextureSize_ != gfx::Size(size)) | |
46 textureGLAdapter(size); | |
47 } | |
48 | |
49 void CompositorOutputCanvas3DTo2DAdapter::initGLAdapter() { | |
50 static const char g_vertex_shader[] = | |
51 "attribute vec4 a_Position;" | |
52 "attribute vec2 a_texCoord;" | |
53 "varying vec2 v_texCoord;" | |
54 "void main() {" | |
55 " gl_Position = a_Position;" | |
56 " v_texCoord = a_texCoord;" | |
57 "}"; | |
58 | |
59 // Minimal texture mapping pixel shader, swizzling appropriately. | |
60 static const char g_fragment_shader[] = | |
61 "precision mediump float;" | |
62 "varying vec2 v_texCoord;" | |
63 "uniform sampler2D s_texture;" | |
64 "void main() {" | |
65 " gl_FragColor = texture2D(s_texture, v_texCoord).bgra;" | |
66 "}"; | |
67 | |
68 const GLfloat attribs[] = { | |
69 -1.0f, -1.0f, | |
70 1.0f, -1.0f, | |
71 -1.0f, 1.0f, | |
72 1.0f, 1.0f, | |
73 0.0f, 0.0f, | |
74 1.0f, 0.0f, | |
75 0.0f, 1.0f, | |
76 1.0f, 1.0f }; | |
77 | |
78 context3d_->makeContextCurrent(); | |
79 | |
80 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
| |
81 context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
82 context3d_->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), attribs, | |
83 GL_STATIC_DRAW); | |
84 | |
85 vertex_shader_ = context3d_->createShader(GL_VERTEX_SHADER); | |
86 context3d_->shaderSource(vertex_shader_, g_vertex_shader); | |
87 context3d_->compileShader(vertex_shader_); | |
88 | |
89 fragment_shader_ = context3d_->createShader(GL_FRAGMENT_SHADER); | |
90 context3d_->shaderSource(fragment_shader_, g_fragment_shader); | |
91 context3d_->compileShader(fragment_shader_); | |
92 | |
93 program_ = context3d_->createProgram(); | |
94 context3d_->attachShader(program_, vertex_shader_); | |
95 context3d_->attachShader(program_, fragment_shader_); | |
96 context3d_->linkProgram(program_); | |
97 | |
98 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
| |
99 | |
100 initialized_ = true; | |
101 } | |
102 | |
103 void CompositorOutputCanvas3DTo2DAdapter::textureGLAdapter( | |
104 const gfx::Size& viewportSize) { | |
105 framebufferTextureSize_ = viewportSize; | |
106 device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, | |
107 viewportSize.width(), viewportSize.height(), true)); | |
108 | |
109 context3d_->makeContextCurrent(); | |
110 context3d_->ensureFramebufferCHROMIUM(); | |
111 framebufferTextureId_ = context3d_->createTexture(); | |
112 context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); | |
113 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
114 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
115 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
116 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
117 | |
118 context3d_->viewport(0, 0, viewportSize.width(), viewportSize.height()); | |
119 context3d_->reshape(viewportSize.width(), viewportSize.height()); | |
120 } | |
121 | |
122 void CompositorOutputCanvas3DTo2DAdapter::frameDrawGLAdapter(void* pixels) { | |
123 if (program_ == GL_ZERO) | |
piman
2012/08/28 03:31:57
if (!program_)
aelias_OOO_until_Jul13
2012/08/28 23:10:58
Done.
| |
124 NOTREACHED(); | |
125 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.
| |
126 NOTREACHED(); | |
127 | |
128 context3d_->makeContextCurrent(); | |
129 context3d_->ensureFramebufferCHROMIUM(); | |
130 context3d_->clearColor(0, 0, 1, 1); | |
131 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().
| |
132 context3d_->clear(GL_COLOR_BUFFER_BIT); | |
133 | |
134 context3d_->activeTexture(GL_TEXTURE0); | |
piman
2012/08/28 03:31:57
move to Initialize() (actually, it's redundant, TE
| |
135 context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); | |
piman
2012/08/28 03:31:57
redundant
| |
136 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | |
137 framebufferTextureSize_.width(), framebufferTextureSize_.height(), | |
138 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | |
139 | |
140 context3d_->useProgram(program_); | |
piman
2012/08/28 03:31:57
move to Initialize()
| |
141 context3d_->uniform1i(texture_uniform_, 0); | |
piman
2012/08/28 03:31:57
move to Initialize()
| |
142 context3d_->activeTexture(GL_TEXTURE0); | |
piman
2012/08/28 03:31:57
redundant
| |
143 context3d_->bindTexture(GL_TEXTURE_2D, framebufferTextureId_); | |
piman
2012/08/28 03:31:57
redundant
| |
144 | |
145 context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
146 context3d_->enableVertexAttribArray(0); | |
147 context3d_->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
148 context3d_->bindAttribLocation(program_, 0, "a_Position"); | |
149 context3d_->enableVertexAttribArray(1); | |
150 context3d_->vertexAttribPointer( | |
151 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); | |
152 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); | |
piman
2012/08/28 03:31:57
Just move all the attrib binding to Initialize.
No
| |
153 | |
154 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
155 | |
156 context3d_->prepareTexture(); | |
157 } | |
OLD | NEW |