Home | History | Annotate | Download | only in gpu
      1 // Copyright 2013 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 "content/common/gpu/devtools_gpu_agent.h"
      6 
      7 #include "base/logging.h"
      8 #include "content/common/devtools_messages.h"
      9 #include "content/common/gpu/gpu_channel.h"
     10 #include "content/common/gpu/gpu_channel_manager.h"
     11 
     12 namespace content {
     13 
     14 DevToolsGpuAgent::DevToolsGpuAgent(GpuChannel* gpu_channel) :
     15     gpu_channel_(gpu_channel),
     16     route_id_(MSG_ROUTING_NONE) {
     17 }
     18 
     19 DevToolsGpuAgent::~DevToolsGpuAgent() {
     20 }
     21 
     22 void DevToolsGpuAgent::StartEventsRecording(int32* route_id) {
     23   DCHECK(CalledOnValidThread());
     24   if (route_id_ != MSG_ROUTING_NONE) {
     25     // Events recording is already in progress, so "fail" the call by
     26     // returning MSG_ROUTING_NONE as the route id.
     27     *route_id = MSG_ROUTING_NONE;
     28     return;
     29   }
     30   route_id_ = gpu_channel_->GenerateRouteID();
     31   *route_id = route_id_;
     32   tasks_.reset(new GpuTaskInfoList());
     33   GpuEventsDispatcher* dispatcher =
     34       gpu_channel_->gpu_channel_manager()->gpu_devtools_events_dispatcher();
     35   dispatcher->AddProcessor(this);
     36 }
     37 
     38 void DevToolsGpuAgent::StopEventsRecording() {
     39   DCHECK(CalledOnValidThread());
     40   if (route_id_ == MSG_ROUTING_NONE)
     41     return;
     42   GpuEventsDispatcher* dispatcher =
     43       gpu_channel_->gpu_channel_manager()->gpu_devtools_events_dispatcher();
     44   dispatcher->RemoveProcessor(this);
     45   route_id_ = MSG_ROUTING_NONE;
     46 }
     47 
     48 void DevToolsGpuAgent::ProcessEvent(
     49     TimeTicks timestamp,
     50     GpuEventsDispatcher::EventPhase phase,
     51     GpuCommandBufferStub* stub) {
     52   DCHECK(CalledOnValidThread());
     53   if (route_id_ == MSG_ROUTING_NONE)
     54     return;
     55 
     56   GpuTaskInfo task;
     57   task.timestamp = (timestamp - TimeTicks()).InSecondsF();
     58   task.phase = phase;
     59   task.foreign = stub->channel() != gpu_channel_;
     60   task.used_gpu_memory_bytes = stub->GetMemoryUsage();
     61 
     62   const int kFlushIntervalMs = 100;
     63   const unsigned kMaxPendingItems = 100;
     64   if (!tasks_->empty() &&
     65       ((timestamp - last_flush_time_).InMilliseconds() >= kFlushIntervalMs ||
     66        tasks_->size() >= kMaxPendingItems)) {
     67     Send(new DevToolsAgentMsg_GpuTasksChunk(route_id_, *tasks_));
     68     tasks_->clear();
     69     last_flush_time_ = timestamp;
     70   }
     71   tasks_->push_back(task);
     72 }
     73 
     74 bool DevToolsGpuAgent::Send(IPC::Message* msg) {
     75   scoped_ptr<IPC::Message> message(msg);
     76   return gpu_channel_ && gpu_channel_->Send(message.release());
     77 }
     78 
     79 }  // namespace content
     80