Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 #include "base/memory/scoped_ptr.h" | |
| 6 #include "base/process_util.h" | |
| 7 #include "chrome/browser/browser_process.h" | |
| 8 #include "content/browser/browser_thread.h" | |
| 9 #include "content/browser/gpu/gpu_thread_host.h" | |
| 10 #include "content/common/gpu/gpu_channel_manager.h" | |
| 11 #include "ipc/ipc_message.h" | |
| 12 #include "ui/gfx/gl/gl_surface.h" | |
| 13 | |
| 14 // This is a helper object to forward messages from the GPU thread to the | |
| 15 // GpuMessageHub. They will then either be forwarded to the UI thread, | |
| 16 // or (in the case of replies) will trigger a callback to the renderer. | |
| 17 class IOThreadSender : public IPC::Channel::Sender { | |
| 18 public: | |
| 19 class SendOnIOThreadTask : public Task { | |
| 20 public: | |
| 21 SendOnIOThreadTask(GpuMessageHub* browser_host, IPC::Message* msg) | |
| 22 : message_hub_(browser_host), | |
| 23 msg_(msg) { | |
| 24 } | |
| 25 | |
| 26 private: | |
| 27 void Run() { | |
| 28 message_hub_->OnMessageReceived(*msg_); | |
| 29 } | |
| 30 | |
| 31 GpuMessageHub* message_hub_; | |
| 32 scoped_ptr<IPC::Message> msg_; | |
| 33 }; | |
| 34 | |
| 35 IOThreadSender(GpuMessageHub* browser_host) | |
| 36 : message_hub_(browser_host) { } | |
|
apatrick_chromium
2011/05/23 22:01:33
drop } to next line.
| |
| 37 | |
| 38 virtual bool Send(IPC::Message* msg) { | |
| 39 bool success = BrowserThread::PostTask( | |
| 40 BrowserThread::IO, | |
| 41 FROM_HERE, | |
| 42 new SendOnIOThreadTask(message_hub_, msg)); | |
| 43 | |
| 44 return success; | |
| 45 } | |
| 46 | |
| 47 private: | |
| 48 GpuMessageHub* message_hub_; | |
| 49 }; | |
| 50 | |
| 51 void ForwardMessageToGpuThread(GpuChannelManager* gpu_channel_manager, | |
| 52 IPC::Message* msg) { | |
| 53 bool success = gpu_channel_manager->OnMessageReceived(*msg); | |
| 54 | |
| 55 // If the message was not handled, it is likely it was intended for the | |
| 56 // GpuChildThread, which does not exist in single process and in process GPU | |
| 57 // mode. | |
| 58 DCHECK(success); | |
| 59 | |
| 60 delete msg; | |
| 61 } | |
| 62 | |
| 63 // static | |
| 64 GpuThreadHost* GpuThreadHost::g_instance = NULL; | |
| 65 | |
| 66 // static | |
| 67 GpuThreadHost* GpuThreadHost::GetInstance() { | |
| 68 // This should get created from channel establishment on the IO thread. | |
| 69 DCHECK(g_instance || BrowserThread::CurrentlyOn(BrowserThread::IO)); | |
| 70 | |
| 71 if (!g_instance) | |
| 72 g_instance = new GpuThreadHost(); | |
| 73 | |
| 74 return g_instance; | |
| 75 } | |
| 76 | |
| 77 // static | |
| 78 void GpuThreadHost::Destroy() { | |
| 79 if (g_instance) { | |
| 80 if (BrowserThread::CurrentlyOn(BrowserThread::IO)) | |
| 81 delete g_instance; | |
| 82 else | |
| 83 BrowserThread::DeleteSoon(BrowserThread::IO, | |
| 84 FROM_HERE, | |
| 85 g_instance); | |
| 86 g_instance = NULL; | |
| 87 } | |
| 88 } | |
| 89 | |
| 90 GpuThreadHost::GpuThreadHost() { | |
| 91 message_hub_.reset(new GpuMessageHub(0)); | |
| 92 message_hub_->SetGPUProcess(base::GetCurrentProcessHandle()); | |
| 93 | |
| 94 browser_sender_ = new IOThreadSender(message_hub_.get()); | |
| 95 | |
| 96 BrowserThread::PostTask( | |
| 97 BrowserThread::GPU, | |
| 98 FROM_HERE, | |
| 99 NewRunnableFunction(&gfx::GLSurface::InitializeOneOff)); | |
| 100 | |
| 101 // Initialize GL on the GPU thread. | |
| 102 // TODO(apatrick): Handle failure to initialize (asynchronously). | |
|
apatrick_chromium
2011/05/23 22:01:33
redundant block
| |
| 103 BrowserThread::PostTask( | |
| 104 BrowserThread::GPU, | |
| 105 FROM_HERE, | |
| 106 NewRunnableFunction(&gfx::GLSurface::InitializeOneOff)); | |
| 107 | |
| 108 gpu_channel_manager_ = new GpuChannelManager( | |
| 109 browser_sender_, | |
| 110 NULL, | |
| 111 BrowserThread::GetMessageLoopProxyForThread(BrowserThread::IO), | |
| 112 g_browser_process->shutdown_event()); | |
| 113 } | |
| 114 | |
| 115 GpuThreadHost::~GpuThreadHost() { | |
| 116 // Ensure these are destroyed on the GPU thread. | |
| 117 if (gpu_channel_manager_) { | |
| 118 BrowserThread::DeleteSoon(BrowserThread::GPU, | |
| 119 FROM_HERE, | |
| 120 gpu_channel_manager_); | |
| 121 gpu_channel_manager_ = NULL; | |
| 122 } | |
| 123 | |
| 124 delete browser_sender_; | |
| 125 } | |
| 126 | |
| 127 bool GpuThreadHost::Send(IPC::Message* message) { | |
| 128 return BrowserThread::PostTask( | |
| 129 BrowserThread::GPU, | |
| 130 FROM_HERE, | |
| 131 NewRunnableFunction(ForwardMessageToGpuThread, | |
| 132 gpu_channel_manager_, | |
| 133 message)); | |
| 134 } | |
| OLD | NEW |