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

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: Moved asserts to RendererGLContext 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
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) {
79 DCHECK(!context_);
73 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::initialize"); 80 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::initialize");
74 webkit_glue::BindSkiaToCommandBufferGL(); 81 webkit_glue::BindSkiaToCommandBufferGL();
75 RenderThread* render_thread = RenderThread::current(); 82 RenderThread* render_thread = RenderThread::current();
76 if (!render_thread) 83 if (!render_thread)
77 return false; 84 return false;
78 GpuChannelHost* host = render_thread->EstablishGpuChannelSync( 85 host_ = render_thread->EstablishGpuChannelSync(
79 content:: 86 content::
80 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE); 87 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE);
81 if (!host) 88 if (!host_)
82 return false; 89 return false;
83 DCHECK(host->state() == GpuChannelHost::kConnected); 90 DCHECK(host_->state() == GpuChannelHost::kConnected);
91
92 const GPUInfo& gpu_info = host_->gpu_info();
93 UMA_HISTOGRAM_ENUMERATION(
94 "GPU.WebGraphicsContext3D_Init_CanLoseContext",
95 attributes.canRecoverFromContextLoss * 2 + gpu_info.can_lose_context,
96 4);
97 if (attributes.canRecoverFromContextLoss == false) {
98 if (gpu_info.can_lose_context)
99 return false;
100 }
101
102 if (web_view && web_view->mainFrame())
103 active_url_ = GURL(web_view->mainFrame()->document().url());
104
105 attributes_ = attributes;
106 render_directly_to_web_view_ = render_directly_to_web_view;
107 if (render_directly_to_web_view_) {
108 RenderView* renderview = RenderView::FromWebView(web_view);
109 if (!renderview)
110 return false;
111 render_view_routing_id_ = renderview->routing_id(),
112 #ifndef WTF_USE_THREADED_COMPOSITING
113 web_view_ = web_view;
114 } else {
115 web_view_ = NULL;
jamesr 2011/08/25 20:05:53 web_view_ is set to NULL in the c'tor so no need t
lain Merrick 2011/08/25 20:11:17 Done.
116 #endif
117 }
118 return true;
119 }
120
121 bool WebGraphicsContext3DCommandBufferImpl::MaybeInitializeGL() {
122 if (context_) {
123 return true;
124 }
125 TRACE_EVENT0("gpu", "WebGfxCtx3DCmdBfrImpl::MaybeInitializeGL");
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());
jamesr 2011/08/25 20:05:53 this lock is held way too long, please tighten it
lain Merrick 2011/08/25 20:11:17 I was thinking of commenting this. I think it's ne
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);
jamesr 2011/08/25 20:05:53 you forgot a lock here
lain Merrick 2011/08/25 20:11:17 Still inside the lock on line 147 (but maybe that'
195 213
196 return true; 214 return true;
197 } 215 }
198 216
199 bool WebGraphicsContext3DCommandBufferImpl::makeContextCurrent() { 217 bool WebGraphicsContext3DCommandBufferImpl::makeContextCurrent() {
218 if (!MaybeInitializeGL())
219 return false;
200 return RendererGLContext::MakeCurrent(context_); 220 return RendererGLContext::MakeCurrent(context_);
201 } 221 }
202 222
203 int WebGraphicsContext3DCommandBufferImpl::width() { 223 int WebGraphicsContext3DCommandBufferImpl::width() {
204 return cached_width_; 224 return cached_width_;
205 } 225 }
206 226
207 int WebGraphicsContext3DCommandBufferImpl::height() { 227 int WebGraphicsContext3DCommandBufferImpl::height() {
208 return cached_height_; 228 return cached_height_;
209 } 229 }
210 230
211 bool WebGraphicsContext3DCommandBufferImpl::isGLES2Compliant() { 231 bool WebGraphicsContext3DCommandBufferImpl::isGLES2Compliant() {
212 return true; 232 return true;
213 } 233 }
214 234
215 bool WebGraphicsContext3DCommandBufferImpl::setParentContext( 235 bool WebGraphicsContext3DCommandBufferImpl::setParentContext(
216 WebGraphicsContext3D* parent_context) { 236 WebGraphicsContext3D* parent_context) {
217 WebGraphicsContext3DCommandBufferImpl* parent_context_impl = 237 WebGraphicsContext3DCommandBufferImpl* parent_context_impl =
218 static_cast<WebGraphicsContext3DCommandBufferImpl*>(parent_context); 238 static_cast<WebGraphicsContext3DCommandBufferImpl*>(parent_context);
219 return context_->SetParent( 239 return context_->SetParent(
220 parent_context_impl ? parent_context_impl->context() : NULL); 240 parent_context_impl ? parent_context_impl->context() : NULL);
221 } 241 }
222 242
223 WebGLId WebGraphicsContext3DCommandBufferImpl::getPlatformTextureId() { 243 WebGLId WebGraphicsContext3DCommandBufferImpl::getPlatformTextureId() {
224 DCHECK(context_);
225 return context_->GetParentTextureId(); 244 return context_->GetParentTextureId();
226 } 245 }
227 246
228 void WebGraphicsContext3DCommandBufferImpl::prepareTexture() { 247 void WebGraphicsContext3DCommandBufferImpl::prepareTexture() {
229 // Copies the contents of the off-screen render target into the texture 248 // Copies the contents of the off-screen render target into the texture
230 // used by the compositor. 249 // used by the compositor.
231 #ifndef WTF_USE_THREADED_COMPOSITING 250 #ifndef WTF_USE_THREADED_COMPOSITING
232 RenderView* renderview = 251 RenderView* renderview =
233 web_view_ ? RenderView::FromWebView(web_view_) : NULL; 252 web_view_ ? RenderView::FromWebView(web_view_) : NULL;
234 if (renderview) 253 if (renderview)
(...skipping 824 matching lines...) Expand 10 before | Expand all | Expand 10 after
1059 } 1078 }
1060 #ifndef WTF_USE_THREADED_COMPOSITING 1079 #ifndef WTF_USE_THREADED_COMPOSITING
1061 RenderView* renderview = 1080 RenderView* renderview =
1062 web_view_ ? RenderView::FromWebView(web_view_) : NULL; 1081 web_view_ ? RenderView::FromWebView(web_view_) : NULL;
1063 if (renderview) 1082 if (renderview)
1064 renderview->OnViewContextSwapBuffersAborted(); 1083 renderview->OnViewContextSwapBuffersAborted();
1065 #endif 1084 #endif
1066 } 1085 }
1067 1086
1068 #endif // defined(ENABLE_GPU) 1087 #endif // defined(ENABLE_GPU)
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698