From 882aa3c950a867ce5485cd2a505d3fd194532714 Mon Sep 17 00:00:00 2001 From: "jamesr@chromium.org" Date: Tue, 12 Aug 2014 21:52:41 +0000 Subject: [PATCH] Make async waiter explicit MojoGLES2CreateContext param Hiding the async_waiter in a static makes it difficult to deal with different threads running potentially different loop types running in the same process. Making this explicit in the create call is much easier. This assumes that callers will want to permantently bind a context with a particular async waiter. In the case that we end up with a caller that wishes to create a context on a thread using one async waiter type and then bind that context to a thread using a different async waiter type we can add another version of MakeCurrent, but that seems like a fairly remote possibility at this point. R=piman@chromium.org Review URL: https://codereview.chromium.org/428413002 Cr-Commit-Position: refs/heads/master@{#289074} git-svn-id: svn://svn.chromium.org/chrome/trunk/src@289074 0039d316-1c4b-4281-b951-d872f2087c98 --- mojo/apps/js/bindings/gl/context.cc | 9 +++-- mojo/apps/js/main.cc | 2 - mojo/cc/context_provider_mojo.cc | 9 +++-- mojo/examples/compositor_app/compositor_app.cc | 2 - .../pepper_container_app/graphics_3d_resource.cc | 4 +- .../pepper_container_app/pepper_container_app.cc | 1 - mojo/examples/sample_app/gles2_client_impl.cc | 10 +++-- mojo/examples/sample_app/sample_app.cc | 2 - mojo/gles2/gles2_impl.cc | 46 ++++++++-------------- mojo/mojo_examples.gypi | 1 - mojo/public/c/gles2/gles2.h | 11 +++--- mojo/public/cpp/gles2/gles2.h | 24 ----------- mojo/public/platform/native/gles2_thunks.cc | 21 +++------- mojo/public/platform/native/gles2_thunks.h | 12 ++---- mojo/services/surfaces/surfaces_impl.cc | 3 -- mojo/services/view_manager/root_view_manager.h | 3 -- 16 files changed, 51 insertions(+), 109 deletions(-) delete mode 100644 mojo/public/cpp/gles2/gles2.h diff --git a/mojo/apps/js/bindings/gl/context.cc b/mojo/apps/js/bindings/gl/context.cc index 250c6f8a01c1..4754d4efda14 100644 --- a/mojo/apps/js/bindings/gl/context.cc +++ b/mojo/apps/js/bindings/gl/context.cc @@ -11,6 +11,7 @@ #include "gin/object_template_builder.h" #include "gin/per_context_data.h" #include "mojo/public/c/gles2/gles2.h" +#include "mojo/public/cpp/environment/environment.h" namespace gin { template<> @@ -154,10 +155,10 @@ Context::Context(v8::Isolate* isolate, v8::Handle context = isolate->GetCurrentContext(); runner_ = gin::PerContextData::From(context)->runner()->GetWeakPtr(); context_lost_callback_.Reset(isolate, context_lost_callback); - context_ = MojoGLES2CreateContext( - handle.value(), - &ContextLostThunk, - this); + context_ = MojoGLES2CreateContext(handle.value(), + &ContextLostThunk, + this, + Environment::GetDefaultAsyncWaiter()); MojoGLES2MakeCurrent(context_); } diff --git a/mojo/apps/js/main.cc b/mojo/apps/js/main.cc index 84231e5971e3..b5d4486736d6 100644 --- a/mojo/apps/js/main.cc +++ b/mojo/apps/js/main.cc @@ -5,7 +5,6 @@ #include "base/message_loop/message_loop.h" #include "gin/public/isolate_holder.h" #include "mojo/apps/js/mojo_runner_delegate.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/public/cpp/system/core.h" #include "mojo/public/cpp/system/macros.h" @@ -37,7 +36,6 @@ void Start(MojoHandle pipe, const std::string& module) { } // namespace mojo extern "C" MOJO_APPS_JS_EXPORT MojoResult CDECL MojoMain(MojoHandle pipe) { - mojo::GLES2Initializer gles2; mojo::apps::Start(pipe, "mojo/apps/js/main"); return MOJO_RESULT_OK; } diff --git a/mojo/cc/context_provider_mojo.cc b/mojo/cc/context_provider_mojo.cc index 1dd62d8a08bd..16c6f8c6ac2b 100644 --- a/mojo/cc/context_provider_mojo.cc +++ b/mojo/cc/context_provider_mojo.cc @@ -5,6 +5,7 @@ #include "mojo/cc/context_provider_mojo.h" #include "base/logging.h" +#include "mojo/public/cpp/environment/environment.h" namespace mojo { @@ -16,10 +17,10 @@ ContextProviderMojo::ContextProviderMojo( bool ContextProviderMojo::BindToCurrentThread() { DCHECK(command_buffer_handle_.is_valid()); - context_ = MojoGLES2CreateContext( - command_buffer_handle_.release().value(), - &ContextLostThunk, - this); + context_ = MojoGLES2CreateContext(command_buffer_handle_.release().value(), + &ContextLostThunk, + this, + Environment::GetDefaultAsyncWaiter()); return !!context_; } diff --git a/mojo/examples/compositor_app/compositor_app.cc b/mojo/examples/compositor_app/compositor_app.cc index fd66b90306a1..513379e6d0fc 100644 --- a/mojo/examples/compositor_app/compositor_app.cc +++ b/mojo/examples/compositor_app/compositor_app.cc @@ -9,7 +9,6 @@ #include "mojo/examples/compositor_app/compositor_host.h" #include "mojo/public/cpp/application/application_delegate.h" #include "mojo/public/cpp/application/application_impl.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/public/cpp/system/core.h" #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h" @@ -53,7 +52,6 @@ class SampleApp : public ApplicationDelegate, public NativeViewportClient { } private: - mojo::GLES2Initializer gles2; NativeViewportPtr viewport_; scoped_ptr host_; DISALLOW_COPY_AND_ASSIGN(SampleApp); diff --git a/mojo/examples/pepper_container_app/graphics_3d_resource.cc b/mojo/examples/pepper_container_app/graphics_3d_resource.cc index 1fa8675638b2..ef6bd76911b6 100644 --- a/mojo/examples/pepper_container_app/graphics_3d_resource.cc +++ b/mojo/examples/pepper_container_app/graphics_3d_resource.cc @@ -8,6 +8,7 @@ #include "mojo/examples/pepper_container_app/mojo_ppapi_globals.h" #include "mojo/examples/pepper_container_app/plugin_instance.h" #include "mojo/public/c/gles2/gles2.h" +#include "mojo/public/cpp/environment/environment.h" #include "ppapi/c/pp_errors.h" namespace mojo { @@ -28,7 +29,8 @@ Graphics3DResource::Graphics3DResource(PP_Instance instance) ScopedMessagePipeHandle pipe = MojoPpapiGlobals::Get()->CreateGLES2Context(); context_ = MojoGLES2CreateContext(pipe.release().value(), &ContextLostThunk, - this); + this, + Environment::GetDefaultAsyncWaiter()); } bool Graphics3DResource::IsBoundGraphics() const { diff --git a/mojo/examples/pepper_container_app/pepper_container_app.cc b/mojo/examples/pepper_container_app/pepper_container_app.cc index 107f6fdf694e..18df756eba48 100644 --- a/mojo/examples/pepper_container_app/pepper_container_app.cc +++ b/mojo/examples/pepper_container_app/pepper_container_app.cc @@ -13,7 +13,6 @@ #include "mojo/examples/pepper_container_app/type_converters.h" #include "mojo/public/cpp/application/application_delegate.h" #include "mojo/public/cpp/application/application_impl.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/public/cpp/system/core.h" #include "mojo/services/public/interfaces/native_viewport/native_viewport.mojom.h" #include "ppapi/c/pp_rect.h" diff --git a/mojo/examples/sample_app/gles2_client_impl.cc b/mojo/examples/sample_app/gles2_client_impl.cc index ad4b93eeaaee..fde074abba13 100644 --- a/mojo/examples/sample_app/gles2_client_impl.cc +++ b/mojo/examples/sample_app/gles2_client_impl.cc @@ -10,6 +10,7 @@ #include #include "mojo/public/c/gles2/gles2.h" +#include "mojo/public/cpp/environment/environment.h" #include "mojo/public/cpp/utility/run_loop.h" namespace examples { @@ -28,10 +29,11 @@ float GetRandomColor() { GLES2ClientImpl::GLES2ClientImpl(mojo::CommandBufferPtr command_buffer) : last_time_(mojo::GetTimeTicksNow()), waiting_to_draw_(false) { - context_ = MojoGLES2CreateContext( - command_buffer.PassMessagePipe().release().value(), - &ContextLostThunk, - this); + context_ = + MojoGLES2CreateContext(command_buffer.PassMessagePipe().release().value(), + &ContextLostThunk, + this, + mojo::Environment::GetDefaultAsyncWaiter()); MojoGLES2MakeCurrent(context_); } diff --git a/mojo/examples/sample_app/sample_app.cc b/mojo/examples/sample_app/sample_app.cc index c60e873ff181..222205a3baa7 100644 --- a/mojo/examples/sample_app/sample_app.cc +++ b/mojo/examples/sample_app/sample_app.cc @@ -9,7 +9,6 @@ #include "mojo/public/cpp/application/application_connection.h" #include "mojo/public/cpp/application/application_delegate.h" #include "mojo/public/cpp/application/application_impl.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/public/cpp/system/core.h" #include "mojo/public/cpp/system/macros.h" #include "mojo/public/cpp/utility/run_loop.h" @@ -70,7 +69,6 @@ class SampleApp : public mojo::ApplicationDelegate, } private: - mojo::GLES2Initializer gles2; scoped_ptr gles2_client_; mojo::NativeViewportPtr viewport_; diff --git a/mojo/gles2/gles2_impl.cc b/mojo/gles2/gles2_impl.cc index 59b427846589..a77405f7a916 100644 --- a/mojo/gles2/gles2_impl.cc +++ b/mojo/gles2/gles2_impl.cc @@ -4,6 +4,8 @@ #include "mojo/public/c/gles2/gles2.h" +#include "base/lazy_instance.h" +#include "base/threading/thread_local.h" #include "gpu/command_buffer/client/gles2_interface.h" #include "mojo/gles2/gles2_context.h" @@ -11,34 +13,20 @@ using mojo::gles2::GLES2Context; namespace { -const MojoAsyncWaiter* g_async_waiter = NULL; -gpu::gles2::GLES2Interface* g_gpu_interface = NULL; +base::LazyInstance >::Leaky + g_gpu_interface; } // namespace extern "C" { - -void MojoGLES2Initialize(const MojoAsyncWaiter* async_waiter) { - DCHECK(!g_async_waiter); - DCHECK(async_waiter); - g_async_waiter = async_waiter; -} - -void MojoGLES2Terminate() { - DCHECK(g_async_waiter); - g_async_waiter = NULL; -} - -MojoGLES2Context MojoGLES2CreateContext( - MojoHandle handle, - MojoGLES2ContextLost lost_callback, - void* closure) { +MojoGLES2Context MojoGLES2CreateContext(MojoHandle handle, + MojoGLES2ContextLost lost_callback, + void* closure, + const MojoAsyncWaiter* async_waiter) { mojo::MessagePipeHandle mph(handle); mojo::ScopedMessagePipeHandle scoped_handle(mph); - scoped_ptr client(new GLES2Context(g_async_waiter, - scoped_handle.Pass(), - lost_callback, - closure)); + scoped_ptr client(new GLES2Context( + async_waiter, scoped_handle.Pass(), lost_callback, closure)); if (!client->Initialize()) client.reset(); return client.release(); @@ -55,12 +43,12 @@ void MojoGLES2MakeCurrent(MojoGLES2Context context) { interface = client->interface(); DCHECK(interface); } - g_gpu_interface = interface; + g_gpu_interface.Get().Set(interface); } void MojoGLES2SwapBuffers() { - assert(g_gpu_interface); - g_gpu_interface->SwapBuffers(); + DCHECK(g_gpu_interface.Get().Get()); + g_gpu_interface.Get().Get()->SwapBuffers(); } void* MojoGLES2GetGLES2Interface(MojoGLES2Context context) { @@ -71,10 +59,10 @@ void* MojoGLES2GetContextSupport(MojoGLES2Context context) { return static_cast(context)->context_support(); } -#define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \ - ReturnType gl##Function PARAMETERS { \ - assert(g_gpu_interface); \ - return g_gpu_interface->Function ARGUMENTS; \ +#define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \ + ReturnType gl##Function PARAMETERS { \ + DCHECK(g_gpu_interface.Get().Get()); \ + return g_gpu_interface.Get().Get()->Function ARGUMENTS; \ } #include "mojo/public/c/gles2/gles2_call_visitor_autogen.h" #undef VISIT_GL_CALL diff --git a/mojo/mojo_examples.gypi b/mojo/mojo_examples.gypi index f933baa4e0f6..d3820735cebf 100644 --- a/mojo/mojo_examples.gypi +++ b/mojo/mojo_examples.gypi @@ -379,7 +379,6 @@ 'mojo_surfaces_bindings', 'mojo_surfaces_app_bindings', 'mojo_surfaces_lib', - '<(mojo_gles2_for_loadable_module)', '<(mojo_system_for_loadable_module)', ], 'sources': [ diff --git a/mojo/public/c/gles2/gles2.h b/mojo/public/c/gles2/gles2.h index 223b1760aad3..36c6f7fdc96e 100644 --- a/mojo/public/c/gles2/gles2.h +++ b/mojo/public/c/gles2/gles2.h @@ -19,12 +19,11 @@ extern "C" { #endif -MOJO_GLES2_EXPORT void MojoGLES2Initialize(const MojoAsyncWaiter* async_waiter); -MOJO_GLES2_EXPORT void MojoGLES2Terminate(void); -MOJO_GLES2_EXPORT MojoGLES2Context MojoGLES2CreateContext( - MojoHandle handle, - MojoGLES2ContextLost lost_callback, - void* closure); +MOJO_GLES2_EXPORT MojoGLES2Context + MojoGLES2CreateContext(MojoHandle handle, + MojoGLES2ContextLost lost_callback, + void* closure, + const MojoAsyncWaiter* async_waiter); MOJO_GLES2_EXPORT void MojoGLES2DestroyContext(MojoGLES2Context context); MOJO_GLES2_EXPORT void MojoGLES2MakeCurrent(MojoGLES2Context context); MOJO_GLES2_EXPORT void MojoGLES2SwapBuffers(void); diff --git a/mojo/public/cpp/gles2/gles2.h b/mojo/public/cpp/gles2/gles2.h deleted file mode 100644 index a408fc701ebe..000000000000 --- a/mojo/public/cpp/gles2/gles2.h +++ /dev/null @@ -1,24 +0,0 @@ -// Copyright 2014 The Chromium Authors. All rights reserved. -// Use of this source code is governed by a BSD-style license that can be -// found in the LICENSE file. - -#ifndef MOJO_PUBLIC_CPP_GLES2_GLES2_H_ -#define MOJO_PUBLIC_CPP_GLES2_GLES2_H_ - -#include "mojo/public/c/gles2/gles2.h" -#include "mojo/public/cpp/environment/environment.h" - -namespace mojo { - -class GLES2Initializer { - public: - explicit GLES2Initializer(const MojoAsyncWaiter* async_waiter = - Environment::GetDefaultAsyncWaiter()) { - MojoGLES2Initialize(async_waiter); - } - ~GLES2Initializer() { MojoGLES2Terminate(); } -}; - -} // namespace mojo - -#endif // MOJO_PUBLIC_CPP_GLES2_GLES2_H_ diff --git a/mojo/public/platform/native/gles2_thunks.cc b/mojo/public/platform/native/gles2_thunks.cc index 239aaf106af6..a8c00a1aebb8 100644 --- a/mojo/public/platform/native/gles2_thunks.cc +++ b/mojo/public/platform/native/gles2_thunks.cc @@ -13,22 +13,13 @@ extern "C" { static MojoGLES2ControlThunks g_control_thunks = {0}; static MojoGLES2ImplThunks g_impl_thunks = {0}; -void MojoGLES2Initialize(const MojoAsyncWaiter* async_waiter) { - assert(g_control_thunks.GLES2Initialize); - g_control_thunks.GLES2Initialize(async_waiter); -} - -void MojoGLES2Terminate() { - assert(g_control_thunks.GLES2Terminate); - g_control_thunks.GLES2Terminate(); -} - -MojoGLES2Context MojoGLES2CreateContext( - MojoHandle handle, - MojoGLES2ContextLost lost_callback, - void* closure) { +MojoGLES2Context MojoGLES2CreateContext(MojoHandle handle, + MojoGLES2ContextLost lost_callback, + void* closure, + const MojoAsyncWaiter* async_waiter) { assert(g_control_thunks.GLES2CreateContext); - return g_control_thunks.GLES2CreateContext(handle, lost_callback, closure); + return g_control_thunks.GLES2CreateContext( + handle, lost_callback, closure, async_waiter); } void MojoGLES2DestroyContext(MojoGLES2Context context) { diff --git a/mojo/public/platform/native/gles2_thunks.h b/mojo/public/platform/native/gles2_thunks.h index e3062438ada7..53375e20a3cf 100644 --- a/mojo/public/platform/native/gles2_thunks.h +++ b/mojo/public/platform/native/gles2_thunks.h @@ -18,12 +18,10 @@ struct MojoGLES2ControlThunks { size_t size; // Should be set to sizeof(MojoGLES2ControlThunks). - void (*GLES2Initialize)(const MojoAsyncWaiter* async_waiter); - void (*GLES2Terminate)(); - MojoGLES2Context (*GLES2CreateContext)( - MojoHandle handle, - MojoGLES2ContextLost lost_callback, - void* closure); + MojoGLES2Context (*GLES2CreateContext)(MojoHandle handle, + MojoGLES2ContextLost lost_callback, + void* closure, + const MojoAsyncWaiter* async_waiter); void (*GLES2DestroyContext)(MojoGLES2Context context); void (*GLES2MakeCurrent)(MojoGLES2Context context); void (*GLES2SwapBuffers)(); @@ -40,8 +38,6 @@ struct MojoGLES2ControlThunks { inline MojoGLES2ControlThunks MojoMakeGLES2ControlThunks() { MojoGLES2ControlThunks gles2_control_thunks = { sizeof(MojoGLES2ControlThunks), - MojoGLES2Initialize, - MojoGLES2Terminate, MojoGLES2CreateContext, MojoGLES2DestroyContext, MojoGLES2MakeCurrent, diff --git a/mojo/services/surfaces/surfaces_impl.cc b/mojo/services/surfaces/surfaces_impl.cc index 141d0c4a0eea..b05e16a0f3f2 100644 --- a/mojo/services/surfaces/surfaces_impl.cc +++ b/mojo/services/surfaces/surfaces_impl.cc @@ -9,7 +9,6 @@ #include "cc/surfaces/display.h" #include "cc/surfaces/surface_id_allocator.h" #include "mojo/cc/context_provider_mojo.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/services/public/cpp/geometry/geometry_type_converters.h" #include "mojo/services/public/cpp/surfaces/surfaces_type_converters.h" @@ -90,8 +89,6 @@ void SurfacesImpl::ReturnResources(const cc::ReturnedResourceArray& resources) { } scoped_ptr SurfacesImpl::CreateOutputSurface() { - static GLES2Initializer* gles2 = new GLES2Initializer; - DCHECK(gles2); return make_scoped_ptr(new cc::OutputSurface( new ContextProviderMojo(command_buffer_handle_.Pass()))); } diff --git a/mojo/services/view_manager/root_view_manager.h b/mojo/services/view_manager/root_view_manager.h index 8656d47b0d3a..366e86bd39f1 100644 --- a/mojo/services/view_manager/root_view_manager.h +++ b/mojo/services/view_manager/root_view_manager.h @@ -10,7 +10,6 @@ #include "base/basictypes.h" #include "base/memory/scoped_ptr.h" #include "mojo/public/cpp/bindings/callback.h" -#include "mojo/public/cpp/gles2/gles2.h" #include "mojo/services/view_manager/view_manager_export.h" namespace aura { @@ -53,8 +52,6 @@ class MOJO_VIEW_MANAGER_EXPORT RootViewManager { RootNodeManager* root_node_manager_; - GLES2Initializer gles_initializer_; - // Returns true if adding the root node's window to |window_tree_host_|. bool in_setup_; -- 2.11.4.GIT