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 (framebufferTextureSize_ != 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 }; | |
piman
2012/08/28 23:26:59
nit: }; on next line.
| |
85 | |
86 context3d_->makeContextCurrent(); | |
87 | |
88 vertex_shader_ = context3d_->createShader(GL_VERTEX_SHADER); | |
89 context3d_->shaderSource(vertex_shader_, g_vertex_shader); | |
90 context3d_->compileShader(vertex_shader_); | |
91 | |
92 fragment_shader_ = context3d_->createShader(GL_FRAGMENT_SHADER); | |
93 context3d_->shaderSource(fragment_shader_, g_fragment_shader); | |
94 context3d_->compileShader(fragment_shader_); | |
95 | |
96 program_ = context3d_->createProgram(); | |
97 context3d_->attachShader(program_, vertex_shader_); | |
98 context3d_->attachShader(program_, fragment_shader_); | |
99 context3d_->linkProgram(program_); | |
100 | |
101 context3d_->useProgram(program_); | |
102 | |
103 int texture_uniform = context3d_->getUniformLocation(program_, "s_texture"); | |
104 context3d_->uniform1i(texture_uniform, 0); | |
105 vertex_buffer_ = context3d_->createBuffer(); | |
106 context3d_->bindBuffer(GL_ARRAY_BUFFER, vertex_buffer_); | |
107 context3d_->bufferData(GL_ARRAY_BUFFER, 16 * sizeof(float), attribs, | |
108 GL_STATIC_DRAW); | |
109 context3d_->enableVertexAttribArray(0); | |
110 context3d_->vertexAttribPointer(0, 2, GL_FLOAT, GL_FALSE, 0, 0); | |
111 context3d_->bindAttribLocation(program_, 0, "a_Position"); | |
piman
2012/08/28 23:26:59
The bindAttribLocation calls need to happen before
aelias_OOO_until_Jul13
2012/08/28 23:53:11
Done.
| |
112 context3d_->enableVertexAttribArray(1); | |
113 context3d_->vertexAttribPointer( | |
114 1, 2, GL_FLOAT, GL_FALSE, 0, 8 * sizeof(GLfloat)); | |
115 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); | |
piman
2012/08/28 23:26:59
This one too.
| |
116 | |
117 context3d_->disable(GL_SCISSOR_TEST); | |
118 | |
119 initialized_ = true; | |
120 } | |
121 | |
122 void CompositorOutputSurfaceSoftwareGLAdapter::Destroy() { | |
123 if (!initialized_) | |
124 return; | |
125 | |
126 context3d_->makeContextCurrent(); | |
127 context3d_->deleteShader(vertex_shader_); | |
128 context3d_->deleteShader(fragment_shader_); | |
129 context3d_->deleteProgram(program_); | |
130 context3d_->deleteBuffer(vertex_buffer_); | |
131 if (framebuffer_texture_id_) | |
132 context3d_->deleteTexture(framebuffer_texture_id_); | |
133 } | |
134 | |
135 void CompositorOutputSurfaceSoftwareGLAdapter::Resize( | |
136 const gfx::Size& viewportSize) { | |
piman
2012/08/28 23:26:59
nit: viewport_size
| |
137 framebufferTextureSize_ = viewportSize; | |
138 device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, | |
139 viewportSize.width(), viewportSize.height(), true)); | |
140 | |
141 context3d_->makeContextCurrent(); | |
142 context3d_->ensureFramebufferCHROMIUM(); | |
143 framebuffer_texture_id_ = context3d_->createTexture(); | |
144 context3d_->bindTexture(GL_TEXTURE_2D, framebuffer_texture_id_); | |
145 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | |
146 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | |
147 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | |
148 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | |
149 | |
150 context3d_->viewport(0, 0, viewportSize.width(), viewportSize.height()); | |
151 context3d_->reshape(viewportSize.width(), viewportSize.height()); | |
152 } | |
153 | |
154 void CompositorOutputSurfaceSoftwareGLAdapter::Draw(void* pixels) { | |
155 if (!initialized_) | |
156 NOTREACHED(); | |
157 if (!framebuffer_texture_id_) | |
158 NOTREACHED(); | |
159 | |
160 context3d_->makeContextCurrent(); | |
161 context3d_->ensureFramebufferCHROMIUM(); | |
162 context3d_->clearColor(0, 0, 1, 1); | |
piman
2012/08/28 23:26:59
you can move this one in Initialize() too.
aelias_OOO_until_Jul13
2012/08/28 23:53:11
Done.
| |
163 context3d_->clear(GL_COLOR_BUFFER_BIT); | |
164 | |
165 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | |
166 framebufferTextureSize_.width(), framebufferTextureSize_.height(), | |
167 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | |
168 | |
169 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
170 | |
171 context3d_->prepareTexture(); | |
172 } | |
OLD | NEW |