summaryrefslogtreecommitdiff
path: root/content/common/gpu/devtools_gpu_agent.cc
blob: e4f5595b37e66cbd70f6aa436320767a7b3728ed (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
// Copyright 2013 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 "content/common/gpu/devtools_gpu_agent.h"

#include "base/logging.h"
#include "content/common/devtools_messages.h"
#include "content/common/gpu/gpu_channel.h"
#include "content/common/gpu/gpu_channel_manager.h"

namespace content {

DevToolsGpuAgent::DevToolsGpuAgent(GpuChannel* gpu_channel) :
    gpu_channel_(gpu_channel),
    route_id_(MSG_ROUTING_NONE) {
}

DevToolsGpuAgent::~DevToolsGpuAgent() {
}

bool DevToolsGpuAgent::StartEventsRecording(int32 route_id) {
  DCHECK(CalledOnValidThread());
  if (route_id_ != MSG_ROUTING_NONE) {
    // Events recording is already in progress, so "fail" the call by
    // returning false.
    return false;
  }
  route_id_ = route_id;
  tasks_.reset(new GpuTaskInfoList());
  GpuEventsDispatcher* dispatcher =
      gpu_channel_->gpu_channel_manager()->gpu_devtools_events_dispatcher();
  dispatcher->AddProcessor(this);
  return true;
}

void DevToolsGpuAgent::StopEventsRecording() {
  DCHECK(CalledOnValidThread());
  if (route_id_ == MSG_ROUTING_NONE)
    return;
  GpuEventsDispatcher* dispatcher =
      gpu_channel_->gpu_channel_manager()->gpu_devtools_events_dispatcher();
  dispatcher->RemoveProcessor(this);
  route_id_ = MSG_ROUTING_NONE;
}

void DevToolsGpuAgent::ProcessEvent(
    TimeTicks timestamp,
    GpuEventsDispatcher::EventPhase phase,
    GpuChannel* channel) {
  DCHECK(CalledOnValidThread());
  if (route_id_ == MSG_ROUTING_NONE)
    return;

  GpuTaskInfo task;
  task.timestamp = (timestamp - TimeTicks()).InSecondsF();
  task.phase = phase;
  task.foreign = channel != gpu_channel_;
  task.gpu_memory_used_bytes = channel->GetMemoryUsage();
  task.gpu_memory_limit_bytes = gpu_channel_->gpu_channel_manager()->
      gpu_memory_manager()->GetMaximumClientAllocation();

  const int kFlushIntervalMs = 100;
  const unsigned kMaxPendingItems = 100;
  if (!tasks_->empty() &&
      ((timestamp - last_flush_time_).InMilliseconds() >= kFlushIntervalMs ||
       tasks_->size() >= kMaxPendingItems)) {
    Send(new DevToolsAgentMsg_GpuTasksChunk(route_id_, *tasks_));
    tasks_->clear();
    last_flush_time_ = timestamp;
  }
  tasks_->push_back(task);
}

bool DevToolsGpuAgent::Send(IPC::Message* msg) {
  scoped_ptr<IPC::Message> message(msg);
  return gpu_channel_ && gpu_channel_->Send(message.release());
}

}  // namespace content