OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "content/renderer/gpu/compositor_software_output_device_gl_adapter.h" | 5 #include "content/renderer/gpu/compositor_software_output_device_gl_adapter.h" |
6 | 6 |
7 #include "base/debug/trace_event.h" | 7 #include "base/debug/trace_event.h" |
8 #include "base/logging.h" | |
9 #include "third_party/skia/include/core/SkBitmap.h" | 8 #include "third_party/skia/include/core/SkBitmap.h" |
| 9 #include "third_party/skia/include/core/SkCanvas.h" |
| 10 #include "third_party/skia/include/core/SkDevice.h" |
10 #include "third_party/skia/include/core/SkPixelRef.h" | 11 #include "third_party/skia/include/core/SkPixelRef.h" |
11 #include "third_party/skia/include/core/SkDevice.h" | |
12 #include "third_party/WebKit/Source/Platform/chromium/public/WebGraphicsContext3
D.h" | |
13 #include "third_party/WebKit/Source/Platform/chromium/public/WebSize.h" | |
14 | 12 |
15 #include <GLES2/gl2.h> | 13 #include <GLES2/gl2.h> |
16 | 14 |
17 using WebKit::WebImage; | |
18 using WebKit::WebGraphicsContext3D; | |
19 using WebKit::WebSize; | |
20 | |
21 namespace content { | 15 namespace content { |
22 | 16 |
23 //------------------------------------------------------------------------------ | |
24 | |
25 CompositorSoftwareOutputDeviceGLAdapter:: | 17 CompositorSoftwareOutputDeviceGLAdapter:: |
26 CompositorSoftwareOutputDeviceGLAdapter(WebGraphicsContext3D* context3D) | 18 CompositorSoftwareOutputDeviceGLAdapter( |
27 : initialized_(false), | 19 WebKit::WebGraphicsContext3D* context3d) |
28 program_(0), | 20 : program_(0), |
29 vertex_shader_(0), | 21 vertex_shader_(0), |
30 fragment_shader_(0), | 22 fragment_shader_(0), |
31 vertex_buffer_(0), | 23 vertex_buffer_(0), |
32 framebuffer_texture_id_(0), | 24 texture_id_(0), |
33 context3d_(context3D), | 25 context3d_(context3d) { |
34 locked_for_write_(false) { | 26 CHECK(context3d); |
35 } | 27 } |
36 | 28 |
37 CompositorSoftwareOutputDeviceGLAdapter:: | 29 CompositorSoftwareOutputDeviceGLAdapter:: |
38 ~CompositorSoftwareOutputDeviceGLAdapter() { | 30 ~CompositorSoftwareOutputDeviceGLAdapter() { |
39 Destroy(); | 31 if (!device_) |
| 32 return; |
| 33 |
| 34 context3d_->makeContextCurrent(); |
| 35 context3d_->deleteShader(vertex_shader_); |
| 36 context3d_->deleteShader(fragment_shader_); |
| 37 context3d_->deleteProgram(program_); |
| 38 context3d_->deleteBuffer(vertex_buffer_); |
| 39 context3d_->deleteTexture(texture_id_); |
40 } | 40 } |
41 | 41 |
42 WebImage* CompositorSoftwareOutputDeviceGLAdapter::Lock(bool forWrite) { | 42 void CompositorSoftwareOutputDeviceGLAdapter::Resize( |
43 locked_for_write_ = forWrite; | 43 const gfx::Size& viewport_size) { |
44 image_ = device_->accessBitmap(forWrite); | 44 if (!device_) |
45 return &image_; | 45 InitShaders(); |
| 46 |
| 47 cc::SoftwareOutputDevice::Resize(viewport_size); |
| 48 |
| 49 context3d_->makeContextCurrent(); |
| 50 context3d_->ensureBackbufferCHROMIUM(); |
| 51 context3d_->viewport(0, 0, viewport_size.width(), viewport_size.height()); |
| 52 context3d_->reshape(viewport_size.width(), viewport_size.height()); |
46 } | 53 } |
47 | 54 |
48 void CompositorSoftwareOutputDeviceGLAdapter::Unlock() { | 55 void CompositorSoftwareOutputDeviceGLAdapter::EndPaint( |
49 if (locked_for_write_) | 56 cc::SoftwareFrameData* frame_data) { |
50 Draw(device_->accessBitmap(false).pixelRef()->pixels()); | 57 DCHECK(device_); |
51 image_.reset(); | 58 DCHECK(frame_data == NULL); |
| 59 |
| 60 TRACE_EVENT0("renderer", "CompositorSoftwareOutputDeviceGLAdapter::EndPaint"); |
| 61 const SkBitmap& bitmap = device_->accessBitmap(false); |
| 62 |
| 63 context3d_->makeContextCurrent(); |
| 64 context3d_->ensureBackbufferCHROMIUM(); |
| 65 context3d_->clear(GL_COLOR_BUFFER_BIT); |
| 66 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, |
| 67 viewport_size_.width(), viewport_size_.height(), |
| 68 0, GL_RGBA, GL_UNSIGNED_BYTE, bitmap.pixelRef()->pixels()); |
| 69 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); |
| 70 context3d_->prepareTexture(); |
52 } | 71 } |
53 | 72 |
54 void CompositorSoftwareOutputDeviceGLAdapter::DidChangeViewportSize( | 73 void CompositorSoftwareOutputDeviceGLAdapter::InitShaders() { |
55 gfx::Size size) { | |
56 if (!initialized_) | |
57 Initialize(); | |
58 | |
59 if (framebuffer_texture_size_ != gfx::Size(size)) | |
60 Resize(size); | |
61 } | |
62 | |
63 void CompositorSoftwareOutputDeviceGLAdapter::Initialize() { | |
64 // Vertex shader that flips the y axis. | 74 // Vertex shader that flips the y axis. |
65 static const char g_vertex_shader[] = | 75 static const char g_vertex_shader[] = |
66 "attribute vec4 a_Position;" | 76 "attribute vec4 a_Position;" |
67 "attribute vec2 a_texCoord;" | 77 "attribute vec2 a_texCoord;" |
68 "varying vec2 v_texCoord;" | 78 "varying vec2 v_texCoord;" |
69 "void main() {" | 79 "void main() {" |
70 " gl_Position = a_Position;" | 80 " gl_Position = a_Position;" |
71 " gl_Position.y = -gl_Position.y;" | 81 " gl_Position.y = -gl_Position.y;" |
72 " v_texCoord = a_texCoord;" | 82 " v_texCoord = a_texCoord;" |
73 "}"; | 83 "}"; |
(...skipping 45 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
119 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); | 129 context3d_->bindAttribLocation(program_, 1, "a_texCoord"); |
120 | 130 |
121 context3d_->linkProgram(program_); | 131 context3d_->linkProgram(program_); |
122 context3d_->useProgram(program_); | 132 context3d_->useProgram(program_); |
123 | 133 |
124 int texture_uniform = context3d_->getUniformLocation(program_, "s_texture"); | 134 int texture_uniform = context3d_->getUniformLocation(program_, "s_texture"); |
125 context3d_->uniform1i(texture_uniform, 0); | 135 context3d_->uniform1i(texture_uniform, 0); |
126 context3d_->disable(GL_SCISSOR_TEST); | 136 context3d_->disable(GL_SCISSOR_TEST); |
127 context3d_->clearColor(0, 0, 1, 1); | 137 context3d_->clearColor(0, 0, 1, 1); |
128 | 138 |
129 initialized_ = true; | 139 texture_id_ = context3d_->createTexture(); |
130 } | 140 context3d_->bindTexture(GL_TEXTURE_2D, texture_id_); |
131 | |
132 void CompositorSoftwareOutputDeviceGLAdapter::Destroy() { | |
133 if (!initialized_) | |
134 return; | |
135 | |
136 context3d_->makeContextCurrent(); | |
137 context3d_->deleteShader(vertex_shader_); | |
138 context3d_->deleteShader(fragment_shader_); | |
139 context3d_->deleteProgram(program_); | |
140 context3d_->deleteBuffer(vertex_buffer_); | |
141 if (framebuffer_texture_id_) | |
142 context3d_->deleteTexture(framebuffer_texture_id_); | |
143 } | |
144 | |
145 void CompositorSoftwareOutputDeviceGLAdapter::Resize( | |
146 const gfx::Size& viewport_size) { | |
147 framebuffer_texture_size_ = viewport_size; | |
148 device_.reset(new SkDevice(SkBitmap::kARGB_8888_Config, | |
149 viewport_size.width(), viewport_size.height(), true)); | |
150 | |
151 context3d_->makeContextCurrent(); | |
152 context3d_->ensureBackbufferCHROMIUM(); | |
153 framebuffer_texture_id_ = context3d_->createTexture(); | |
154 context3d_->bindTexture(GL_TEXTURE_2D, framebuffer_texture_id_); | |
155 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); | 141 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_LINEAR); |
156 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); | 142 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_MAG_FILTER, GL_LINEAR); |
157 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); | 143 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_S, GL_CLAMP_TO_EDGE); |
158 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); | 144 context3d_->texParameteri(GL_TEXTURE_2D, GL_TEXTURE_WRAP_T, GL_CLAMP_TO_EDGE); |
159 | |
160 context3d_->viewport(0, 0, viewport_size.width(), viewport_size.height()); | |
161 context3d_->reshape(viewport_size.width(), viewport_size.height()); | |
162 } | |
163 | |
164 void CompositorSoftwareOutputDeviceGLAdapter::Draw(void* pixels) { | |
165 TRACE_EVENT0("renderer", "CompositorSoftwareOutputDeviceGLAdapter::Draw"); | |
166 if (!initialized_) | |
167 NOTREACHED(); | |
168 if (!framebuffer_texture_id_) | |
169 NOTREACHED(); | |
170 | |
171 context3d_->makeContextCurrent(); | |
172 context3d_->ensureBackbufferCHROMIUM(); | |
173 context3d_->clear(GL_COLOR_BUFFER_BIT); | |
174 | |
175 context3d_->texImage2D(GL_TEXTURE_2D, 0, GL_RGBA, | |
176 framebuffer_texture_size_.width(), framebuffer_texture_size_.height(), | |
177 0, GL_RGBA, GL_UNSIGNED_BYTE, pixels); | |
178 | |
179 context3d_->drawArrays(GL_TRIANGLE_STRIP, 0, 4); | |
180 | |
181 context3d_->prepareTexture(); | |
182 } | 145 } |
183 | 146 |
184 } // namespace content | 147 } // namespace content |
OLD | NEW |