Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(466)

Side by Side Diff: content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.cc

Issue 7713015: Split WebGraphicsContext3DCommandBufferImpl::initialize() into two stages. (Closed) Base URL: http://src.chromium.org/svn/trunk/src/
Patch Set: Added mutex for g_all_contexts Created 9 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 1 // Copyright (c) 2011 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 #if defined(ENABLE_GPU) 5 #if defined(ENABLE_GPU)
6 6
7 #include "content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h" 7 #include "content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h"
8 8
9 #include "gpu/GLES2/gl2.h" 9 #include "gpu/GLES2/gl2.h"
10 #ifndef GL_GLEXT_PROTOTYPES 10 #ifndef GL_GLEXT_PROTOTYPES
11 #define GL_GLEXT_PROTOTYPES 1 11 #define GL_GLEXT_PROTOTYPES 1
12 #endif 12 #endif
13 #include "gpu/GLES2/gl2ext.h" 13 #include "gpu/GLES2/gl2ext.h"
14 14
15 #include <algorithm> 15 #include <algorithm>
16 #include <set> 16 #include <set>
17 17
18 #include "base/lazy_instance.h" 18 #include "base/lazy_instance.h"
19 #include "base/string_tokenizer.h" 19 #include "base/string_tokenizer.h"
20 #include "base/command_line.h" 20 #include "base/command_line.h"
21 #include "base/debug/trace_event.h" 21 #include "base/debug/trace_event.h"
22 #include "base/logging.h" 22 #include "base/logging.h"
23 #include "base/metrics/histogram.h" 23 #include "base/metrics/histogram.h"
24 #include "base/synchronization/lock.h"
24 #include "content/common/content_switches.h" 25 #include "content/common/content_switches.h"
25 #include "content/renderer/gpu/gpu_channel_host.h" 26 #include "content/renderer/gpu/gpu_channel_host.h"
26 #include "content/renderer/render_thread.h" 27 #include "content/renderer/render_thread.h"
27 #include "content/renderer/render_view.h" 28 #include "content/renderer/render_view.h"
28 #include "gpu/command_buffer/client/gles2_implementation.h" 29 #include "gpu/command_buffer/client/gles2_implementation.h"
29 #include "gpu/command_buffer/common/constants.h" 30 #include "gpu/command_buffer/common/constants.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h" 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
32 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h" 33 #include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
33 #include "webkit/glue/gl_bindings_skia_cmd_buffer.h" 34 #include "webkit/glue/gl_bindings_skia_cmd_buffer.h"
34 35
36 static base::LazyInstance<base::Lock>
37 g_all_contexts_lock(base::LINKER_INITIALIZED);
35 static base::LazyInstance<std::set<WebGraphicsContext3DCommandBufferImpl*> > 38 static base::LazyInstance<std::set<WebGraphicsContext3DCommandBufferImpl*> >
36 g_all_contexts(base::LINKER_INITIALIZED); 39 g_all_contexts(base::LINKER_INITIALIZED);
37 40
38 WebGraphicsContext3DCommandBufferImpl::WebGraphicsContext3DCommandBufferImpl() 41 WebGraphicsContext3DCommandBufferImpl::WebGraphicsContext3DCommandBufferImpl()
39 : context_(NULL), 42 : context_(NULL),
40 gl_(NULL), 43 gl_(NULL),
41 #ifndef WTF_USE_THREADED_COMPOSITING 44 #ifndef WTF_USE_THREADED_COMPOSITING
42 web_view_(NULL), 45 web_view_(NULL),
43 #endif 46 #endif
44 #if defined(OS_MACOSX) 47 #if defined(OS_MACOSX)
45 plugin_handle_(NULL), 48 plugin_handle_(NULL),
46 #endif // defined(OS_MACOSX) 49 #endif // defined(OS_MACOSX)
47 context_lost_callback_(0), 50 context_lost_callback_(0),
48 context_lost_reason_(GL_NO_ERROR), 51 context_lost_reason_(GL_NO_ERROR),
49 swapbuffers_complete_callback_(0), 52 swapbuffers_complete_callback_(0),
50 cached_width_(0), 53 cached_width_(0),
51 cached_height_(0), 54 cached_height_(0),
52 bound_fbo_(0) { 55 bound_fbo_(0) {
53 } 56 }
54 57
55 WebGraphicsContext3DCommandBufferImpl:: 58 WebGraphicsContext3DCommandBufferImpl::
56 ~WebGraphicsContext3DCommandBufferImpl() { 59 ~WebGraphicsContext3DCommandBufferImpl() {
57 g_all_contexts.Pointer()->erase(this); 60 {
61 base::AutoLock lock(g_all_contexts_lock.Get());
62 g_all_contexts.Pointer()->erase(this);
63 }
58 delete context_; 64 delete context_;
59 } 65 }
60 66
61 // This string should only be passed for WebGL contexts. Nothing ELSE!!! 67 // This string should only be passed for WebGL contexts. Nothing ELSE!!!
62 // Compositor contexts, Canvas2D contexts, Pepper Contexts, nor any other use of 68 // Compositor contexts, Canvas2D contexts, Pepper Contexts, nor any other use of
63 // a context should not pass this string. 69 // a context should not pass this string.
64 static const char* kWebGLPreferredGLExtensions = 70 static const char* kWebGLPreferredGLExtensions =
65 "GL_OES_packed_depth_stencil " 71 "GL_OES_packed_depth_stencil "
66 "GL_OES_depth24 " 72 "GL_OES_depth24 "
67 "GL_CHROMIUM_webglsl"; 73 "GL_CHROMIUM_webglsl";
68 74
69 bool WebGraphicsContext3DCommandBufferImpl::initialize( 75 bool WebGraphicsContext3DCommandBufferImpl::initialize(
70 WebGraphicsContext3D::Attributes attributes, 76 WebGraphicsContext3D::Attributes attributes,
71 WebKit::WebView* web_view, 77 WebKit::WebView* web_view,
72 bool render_directly_to_web_view) { 78 bool render_directly_to_web_view) {
73 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::initialize"); 79 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::initialize");
74 webkit_glue::BindSkiaToCommandBufferGL(); 80 webkit_glue::BindSkiaToCommandBufferGL();
75 RenderThread* render_thread = RenderThread::current(); 81 RenderThread* render_thread = RenderThread::current();
76 if (!render_thread) 82 if (!render_thread)
77 return false; 83 return false;
78 GpuChannelHost* host = render_thread->EstablishGpuChannelSync( 84 host_ = render_thread->EstablishGpuChannelSync(
79 content:: 85 content::
80 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE); 86 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE);
81 if (!host) 87 if (!host_)
82 return false; 88 return false;
83 DCHECK(host->state() == GpuChannelHost::kConnected); 89 DCHECK(host_->state() == GpuChannelHost::kConnected);
90
91 const GPUInfo& gpu_info = host_->gpu_info();
92 UMA_HISTOGRAM_ENUMERATION(
93 "GPU.WebGraphicsContext3D_Init_CanLoseContext",
94 attributes.canRecoverFromContextLoss * 2 + gpu_info.can_lose_context,
95 4);
96 if (attributes.canRecoverFromContextLoss == false) {
97 if (gpu_info.can_lose_context)
98 return false;
99 }
100
101 if (web_view && web_view->mainFrame())
102 active_url_ = GURL(web_view->mainFrame()->document().url());
103
104 attributes_ = attributes;
105 render_directly_to_web_view_ = render_directly_to_web_view;
106 if (render_directly_to_web_view_) {
107 RenderView* renderview = RenderView::FromWebView(web_view);
108 if (!renderview)
109 return false;
110 render_view_routing_id_ = renderview->routing_id(),
111 #ifndef WTF_USE_THREADED_COMPOSITING
112 web_view_ = web_view;
113 } else {
114 web_view_ = NULL;
115 #endif
116 }
117
118 context_ = NULL;
no sievers 2011/08/24 02:50:44 Maybe rather DCHECK(context_ == NULL) at the begin
lain Merrick 2011/08/24 19:52:05 Done.
119 gl_ = NULL;
120 return true;
121 }
122
123 bool WebGraphicsContext3DCommandBufferImpl::InitializeGLES2() {
124 DCHECK(!context_);
125 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::InitializeGLES2");
84 126
85 // Convert WebGL context creation attributes into RendererGLContext / EGL size 127 // Convert WebGL context creation attributes into RendererGLContext / EGL size
86 // requests. 128 // requests.
87 const int alpha_size = attributes.alpha ? 8 : 0; 129 const int alpha_size = attributes_.alpha ? 8 : 0;
88 const int depth_size = attributes.depth ? 24 : 0; 130 const int depth_size = attributes_.depth ? 24 : 0;
89 const int stencil_size = attributes.stencil ? 8 : 0; 131 const int stencil_size = attributes_.stencil ? 8 : 0;
90 const int samples = attributes.antialias ? 4 : 0; 132 const int samples = attributes_.antialias ? 4 : 0;
91 const int sample_buffers = attributes.antialias ? 1 : 0; 133 const int sample_buffers = attributes_.antialias ? 1 : 0;
92 const int32 attribs[] = { 134 const int32 attribs[] = {
93 RendererGLContext::ALPHA_SIZE, alpha_size, 135 RendererGLContext::ALPHA_SIZE, alpha_size,
94 RendererGLContext::DEPTH_SIZE, depth_size, 136 RendererGLContext::DEPTH_SIZE, depth_size,
95 RendererGLContext::STENCIL_SIZE, stencil_size, 137 RendererGLContext::STENCIL_SIZE, stencil_size,
96 RendererGLContext::SAMPLES, samples, 138 RendererGLContext::SAMPLES, samples,
97 RendererGLContext::SAMPLE_BUFFERS, sample_buffers, 139 RendererGLContext::SAMPLE_BUFFERS, sample_buffers,
98 RendererGLContext::NONE, 140 RendererGLContext::NONE,
99 }; 141 };
100 142
101 const char* preferred_extensions = attributes.noExtensions ?
102 kWebGLPreferredGLExtensions : "*";
103
104 const GPUInfo& gpu_info = host->gpu_info();
105 UMA_HISTOGRAM_ENUMERATION(
106 "GPU.WebGraphicsContext3D_Init_CanLoseContext",
107 attributes.canRecoverFromContextLoss * 2 + gpu_info.can_lose_context,
108 4);
109 if (attributes.canRecoverFromContextLoss == false) {
110 if (gpu_info.can_lose_context)
111 return false;
112 }
113
114 GURL active_url;
115 if (web_view && web_view->mainFrame())
116 active_url = GURL(web_view->mainFrame()->document().url());
117
118 // HACK: Assume this is a WebGL context by looking for the noExtensions 143 // HACK: Assume this is a WebGL context by looking for the noExtensions
119 // attribute. WebGL contexts must not go in the share group because they 144 // attribute. WebGL contexts must not go in the share group because they
120 // rely on destruction of the context to clean up owned resources. Putting 145 // rely on destruction of the context to clean up owned resources. Putting
121 // them in a share group would prevent this from happening. 146 // them in a share group would prevent this from happening.
147 base::AutoLock lock(g_all_contexts_lock.Get());
122 RendererGLContext* share_group = NULL; 148 RendererGLContext* share_group = NULL;
123 if (!attributes.noExtensions) { 149 if (!attributes_.noExtensions) {
124 share_group = g_all_contexts.Pointer()->empty() ? 150 share_group = g_all_contexts.Pointer()->empty() ?
125 NULL : (*g_all_contexts.Pointer()->begin())->context_; 151 NULL : (*g_all_contexts.Pointer()->begin())->context_;
126 } 152 }
127 153
128 render_directly_to_web_view_ = render_directly_to_web_view; 154 const char* preferred_extensions = attributes_.noExtensions ?
129 if (render_directly_to_web_view) { 155 kWebGLPreferredGLExtensions : "*";
130 #ifndef WTF_USE_THREADED_COMPOSITING 156
131 RenderView* renderview = RenderView::FromWebView(web_view); 157 if (render_directly_to_web_view_) {
132 if (!renderview)
133 return false;
134 web_view_ = web_view;
135 #endif
136 context_ = RendererGLContext::CreateViewContext( 158 context_ = RendererGLContext::CreateViewContext(
137 host, 159 host_,
138 renderview->routing_id(), 160 render_view_routing_id_,
139 !attributes.noExtensions, 161 !attributes_.noExtensions,
140 share_group, 162 share_group,
141 preferred_extensions, 163 preferred_extensions,
142 attribs, 164 attribs,
143 active_url); 165 active_url_);
144 if (context_) { 166 if (context_) {
145 context_->SetSwapBuffersCallback( 167 context_->SetSwapBuffersCallback(
146 NewCallback(this, 168 NewCallback(this,
147 &WebGraphicsContext3DCommandBufferImpl::OnSwapBuffersComplete)); 169 &WebGraphicsContext3DCommandBufferImpl::OnSwapBuffersComplete));
148 } 170 }
149 } else { 171 } else {
150 context_ = RendererGLContext::CreateOffscreenContext( 172 context_ = RendererGLContext::CreateOffscreenContext(
151 host, 173 host_,
152 gfx::Size(1, 1), 174 gfx::Size(1, 1),
153 !attributes.noExtensions, 175 !attributes_.noExtensions,
154 share_group, 176 share_group,
155 preferred_extensions, 177 preferred_extensions,
156 attribs, 178 attribs,
157 active_url); 179 active_url_);
158 #ifndef WTF_USE_THREADED_COMPOSITING
159 web_view_ = NULL;
160 #endif
161 } 180 }
162 if (!context_) 181 if (!context_)
163 return false; 182 return false;
164 183
165 gl_ = context_->GetImplementation(); 184 gl_ = context_->GetImplementation();
166 context_->SetContextLostCallback( 185 context_->SetContextLostCallback(
167 NewCallback(this, 186 NewCallback(this,
168 &WebGraphicsContext3DCommandBufferImpl::OnContextLost)); 187 &WebGraphicsContext3DCommandBufferImpl::OnContextLost));
169 188
170 // TODO(gman): Remove this. 189 // TODO(gman): Remove this.
171 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 190 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
172 if (command_line.HasSwitch(switches::kDisableGLSLTranslator)) { 191 if (command_line.HasSwitch(switches::kDisableGLSLTranslator)) {
173 context_->DisableShaderTranslation(); 192 context_->DisableShaderTranslation();
174 } 193 }
175 194
176 // Set attributes_ from created offscreen context. 195 // Set attributes_ from created offscreen context.
177 { 196 {
178 attributes_ = attributes;
179 GLint alpha_bits = 0; 197 GLint alpha_bits = 0;
180 getIntegerv(GL_ALPHA_BITS, &alpha_bits); 198 getIntegerv(GL_ALPHA_BITS, &alpha_bits);
181 attributes_.alpha = alpha_bits > 0; 199 attributes_.alpha = alpha_bits > 0;
182 GLint depth_bits = 0; 200 GLint depth_bits = 0;
183 getIntegerv(GL_DEPTH_BITS, &depth_bits); 201 getIntegerv(GL_DEPTH_BITS, &depth_bits);
184 attributes_.depth = depth_bits > 0; 202 attributes_.depth = depth_bits > 0;
185 GLint stencil_bits = 0; 203 GLint stencil_bits = 0;
186 getIntegerv(GL_STENCIL_BITS, &stencil_bits); 204 getIntegerv(GL_STENCIL_BITS, &stencil_bits);
187 attributes_.stencil = stencil_bits > 0; 205 attributes_.stencil = stencil_bits > 0;
188 GLint samples = 0; 206 GLint samples = 0;
189 getIntegerv(GL_SAMPLES, &samples); 207 getIntegerv(GL_SAMPLES, &samples);
190 attributes_.antialias = samples > 0; 208 attributes_.antialias = samples > 0;
191 } 209 }
192 210
193 if (!attributes.noExtensions) 211 if (!attributes_.noExtensions)
194 g_all_contexts.Pointer()->insert(this); 212 g_all_contexts.Pointer()->insert(this);
195 213
196 return true; 214 return true;
197 } 215 }
198 216
199 bool WebGraphicsContext3DCommandBufferImpl::makeContextCurrent() { 217 bool WebGraphicsContext3DCommandBufferImpl::makeContextCurrent() {
218 if (!context_) {
219 if (!InitializeGLES2()) {
220 return false;
221 }
222 }
200 return RendererGLContext::MakeCurrent(context_); 223 return RendererGLContext::MakeCurrent(context_);
201 } 224 }
202 225
203 int WebGraphicsContext3DCommandBufferImpl::width() { 226 int WebGraphicsContext3DCommandBufferImpl::width() {
204 return cached_width_; 227 return cached_width_;
205 } 228 }
206 229
207 int WebGraphicsContext3DCommandBufferImpl::height() { 230 int WebGraphicsContext3DCommandBufferImpl::height() {
208 return cached_height_; 231 return cached_height_;
209 } 232 }
210 233
211 bool WebGraphicsContext3DCommandBufferImpl::isGLES2Compliant() { 234 bool WebGraphicsContext3DCommandBufferImpl::isGLES2Compliant() {
212 return true; 235 return true;
213 } 236 }
214 237
215 bool WebGraphicsContext3DCommandBufferImpl::setParentContext( 238 bool WebGraphicsContext3DCommandBufferImpl::setParentContext(
216 WebGraphicsContext3D* parent_context) { 239 WebGraphicsContext3D* parent_context) {
217 WebGraphicsContext3DCommandBufferImpl* parent_context_impl = 240 WebGraphicsContext3DCommandBufferImpl* parent_context_impl =
218 static_cast<WebGraphicsContext3DCommandBufferImpl*>(parent_context); 241 static_cast<WebGraphicsContext3DCommandBufferImpl*>(parent_context);
219 return context_->SetParent( 242 return context_->SetParent(
220 parent_context_impl ? parent_context_impl->context() : NULL); 243 parent_context_impl ? parent_context_impl->context() : NULL);
221 } 244 }
222 245
223 WebGLId WebGraphicsContext3DCommandBufferImpl::getPlatformTextureId() { 246 WebGLId WebGraphicsContext3DCommandBufferImpl::getPlatformTextureId() {
224 DCHECK(context_);
225 return context_->GetParentTextureId(); 247 return context_->GetParentTextureId();
226 } 248 }
227 249
228 void WebGraphicsContext3DCommandBufferImpl::prepareTexture() { 250 void WebGraphicsContext3DCommandBufferImpl::prepareTexture() {
229 // Copies the contents of the off-screen render target into the texture 251 // Copies the contents of the off-screen render target into the texture
230 // used by the compositor. 252 // used by the compositor.
231 #ifndef WTF_USE_THREADED_COMPOSITING 253 #ifndef WTF_USE_THREADED_COMPOSITING
232 RenderView* renderview = 254 RenderView* renderview =
233 web_view_ ? RenderView::FromWebView(web_view_) : NULL; 255 web_view_ ? RenderView::FromWebView(web_view_) : NULL;
234 if (renderview) 256 if (renderview)
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after
1059 } 1081 }
1060 #ifndef WTF_USE_THREADED_COMPOSITING 1082 #ifndef WTF_USE_THREADED_COMPOSITING
1061 RenderView* renderview = 1083 RenderView* renderview =
1062 web_view_ ? RenderView::FromWebView(web_view_) : NULL; 1084 web_view_ ? RenderView::FromWebView(web_view_) : NULL;
1063 if (renderview) 1085 if (renderview)
1064 renderview->OnViewContextSwapBuffersAborted(); 1086 renderview->OnViewContextSwapBuffersAborted();
1065 #endif 1087 #endif
1066 } 1088 }
1067 1089
1068 #endif // defined(ENABLE_GPU) 1090 #endif // defined(ENABLE_GPU)
OLDNEW
« no previous file with comments | « content/renderer/gpu/webgraphicscontext3d_command_buffer_impl.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698