summaryrefslogtreecommitdiffstats
path: root/chrome/gpu/gpu_channel.cc
blob: 25bb9515c9b4dee9e2e3b67011e6ddf894169645 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
// Copyright (c) 2010 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.

#include "chrome/gpu/gpu_channel.h"

#include "base/command_line.h"
#include "base/lock.h"
#include "base/process_util.h"
#include "base/string_util.h"
#include "base/waitable_event.h"
#include "build/build_config.h"
#include "chrome/common/child_process.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/gpu_messages.h"
#include "chrome/gpu/gpu_thread.h"

#if defined(OS_POSIX)
#include "ipc/ipc_channel_posix.h"
#endif

namespace {
class GpuReleaseTask : public Task {
 public:
  void Run() {
    ChildProcess::current()->ReleaseProcess();
  }
};

typedef base::hash_map<std::string, scoped_refptr<GpuChannel> >
    GpuChannelMap;

// How long we wait before releasing the GPU process.
const int kGpuReleaseTimeMS = 10000;

GpuChannelMap g_gpu_channels;
}  // namespace anonymous

GpuChannel* GpuChannel::EstablishGpuChannel(int renderer_id) {
  // Map renderer ID to a (single) channel to that process.
  std::string channel_name = StringPrintf(
      "%d.r%d", base::GetCurrentProcId(), renderer_id);

  scoped_refptr<GpuChannel> channel;

  GpuChannelMap::const_iterator iter = g_gpu_channels.find(channel_name);
  if (iter == g_gpu_channels.end()) {
    channel = new GpuChannel;
  } else {
    channel = iter->second;
  }

  DCHECK(channel != NULL);

  if (!channel->channel_.get()) {
    if (channel->Init(channel_name)) {
      g_gpu_channels[channel_name] = channel;
    } else {
      channel = NULL;
    }
  }

  return channel.get();
}

GpuChannel::GpuChannel()
    : renderer_id_(-1)
#if defined(OS_POSIX)
    , renderer_fd_(-1)
#endif
{
  ChildProcess::current()->AddRefProcess();
  const CommandLine* command_line = CommandLine::ForCurrentProcess();
  log_messages_ = command_line->HasSwitch(switches::kLogPluginMessages);
}

GpuChannel::~GpuChannel() {
#if defined(OS_POSIX)
  // If we still have the renderer FD, close it.
  if (renderer_fd_ != -1) {
    close(renderer_fd_);
  }
#endif
  ChildProcess::current()->io_message_loop()->PostDelayedTask(
      FROM_HERE,
      new GpuReleaseTask(),
      kGpuReleaseTimeMS);
}

void GpuChannel::OnChannelConnected(int32 peer_pid) {
  if (!renderer_process_.Open(peer_pid)) {
    NOTREACHED();
  }
}

void GpuChannel::OnMessageReceived(const IPC::Message& message) {
  if (log_messages_) {
    LOG(INFO) << "received message @" << &message << " on channel @" << this
              << " with type " << message.type();
  }

  if (message.routing_id() == MSG_ROUTING_CONTROL) {
    OnControlMessageReceived(message);
  } else {
    // The sender should know not to route messages to an object after it
    // has been destroyed.
    CHECK(router_.RouteMessage(message));
  }
}

void GpuChannel::OnChannelError() {
  // Destroy channel. This will cause the channel to be recreated if another
  // attempt is made to establish a connection from the corresponding renderer.
  channel_.reset();

  // Close renderer process handle.
  renderer_process_.Close();

#if defined(ENABLE_GPU)
  // Destroy all the stubs on this channel.
  for (size_t i = 0; i < stubs_.size(); ++i) {
    router_.RemoveRoute(stubs_[i]->route_id());
  }
  stubs_.clear();
#endif
}

bool GpuChannel::Send(IPC::Message* message) {
  if (log_messages_) {
    LOG(INFO) << "sending message @" << message << " on channel @" << this
              << " with type " << message->type();
  }

  if (!channel_.get()) {
    delete message;
    return false;
  }

  return channel_->Send(message);
}

void GpuChannel::OnControlMessageReceived(const IPC::Message& msg) {
  IPC_BEGIN_MESSAGE_MAP(GpuChannel, msg)
    IPC_MESSAGE_HANDLER(GpuChannelMsg_CreateCommandBuffer,
        OnCreateCommandBuffer)
    IPC_MESSAGE_HANDLER(GpuChannelMsg_DestroyCommandBuffer,
        OnDestroyCommandBuffer)
    IPC_MESSAGE_UNHANDLED_ERROR()
  IPC_END_MESSAGE_MAP()
}

int GpuChannel::GenerateRouteID() {
  static int last_id = 0;
  return ++last_id;
}

void GpuChannel::OnCreateCommandBuffer(int* route_id) {
#if defined(ENABLE_GPU)
  *route_id = GenerateRouteID();
  scoped_refptr<GpuCommandBufferStub> stub = new GpuCommandBufferStub(
      this, *route_id);
  router_.AddRoute(*route_id, stub);
  stubs_[*route_id] = stub;
#else
  *route_id = 0;
#endif
}

void GpuChannel::OnDestroyCommandBuffer(int route_id) {
#if defined(ENABLE_GPU)
  StubMap::iterator it = stubs_.find(route_id);
  DCHECK(it != stubs_.end());
  stubs_.erase(it);
  router_.RemoveRoute(route_id);
#endif
}

bool GpuChannel::Init(const std::string& channel_name) {
  channel_name_ = channel_name;
  channel_.reset(new IPC::SyncChannel(
      channel_name, IPC::Channel::MODE_SERVER, this, NULL,
      ChildProcess::current()->io_message_loop(), false,
      ChildProcess::current()->GetShutDownEvent()));
  return true;
}