summaryrefslogtreecommitdiffstats
path: root/chrome/renderer/command_buffer_proxy.h
blob: 9d0fcb26f321228e1b0c63fd487e8407a3b2a4f9 (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
// Copyright (c) 2009 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 CHROME_RENDERER_COMMAND_BUFFER_PROXY_H_
#define CHROME_RENDERER_COMMAND_BUFFER_PROXY_H_
#pragma once

#if defined(ENABLE_GPU)

#include <map>
#include <queue>

#include "base/linked_ptr.h"
#include "base/ref_counted.h"
#include "base/scoped_ptr.h"
#include "base/shared_memory.h"
#include "base/task.h"
#include "gfx/size.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "ipc/ipc_channel.h"
#include "ipc/ipc_message.h"

class PluginChannelHost;

// Client side proxy that forwards messages synchronously to a
// CommandBufferStub.
class CommandBufferProxy : public gpu::CommandBuffer,
                           public IPC::Channel::Listener,
                           public IPC::Message::Sender {
 public:
  CommandBufferProxy(IPC::Channel::Sender* channel, int route_id);
  virtual ~CommandBufferProxy();

  // IPC::Channel::Listener implementation:
  virtual void OnMessageReceived(const IPC::Message& message);
  virtual void OnChannelError();

  // IPC::Message::Sender implementation:
  virtual bool Send(IPC::Message* msg);

  int route_id() const { return route_id_; }

  // CommandBuffer implementation:
  virtual bool Initialize(int32 size);
  virtual gpu::Buffer GetRingBuffer();
  virtual State GetState();
  virtual State Flush(int32 put_offset);
  virtual void SetGetOffset(int32 get_offset);
  virtual int32 CreateTransferBuffer(size_t size);
  virtual void DestroyTransferBuffer(int32 id);
  virtual gpu::Buffer GetTransferBuffer(int32 handle);
  virtual void SetToken(int32 token);
  virtual void SetParseError(gpu::error::Error error);

  // Asynchronously resizes an offscreen frame buffer.
  void ResizeOffscreenFrameBuffer(const gfx::Size& size);

  // Set a task that will be invoked the next time the window becomes invalid
  // and needs to be repainted. Takes ownership of task.
  void SetNotifyRepaintTask(Task* task) {
    notify_repaint_task_.reset(task);
  }

#if defined(OS_MACOSX)
  virtual void SetWindowSize(const gfx::Size& size);
#endif

  // Get the last state received from the service without synchronizing.
  State GetLastState() {
    return last_state_;
  }

  // Get the state asynchronously. The task is posted when the state is
  // updated. Takes ownership of the task object.
  void AsyncGetState(Task* completion_task);

  // Flush the command buffer asynchronously. The task is posted when the flush
  // completes. Takes ownership of the task object.
  void AsyncFlush(int32 put_offset, Task* completion_task);

 private:
  // Message handlers:
  void OnUpdateState(gpu::CommandBuffer::State state);
  void OnNotifyRepaint();

  // As with the service, the client takes ownership of the ring buffer.
  int32 num_entries_;
  scoped_ptr<base::SharedMemory> ring_buffer_;

  // Local cache of id to transfer buffer mapping.
  typedef std::map<int32, gpu::Buffer> TransferBufferMap;
  TransferBufferMap transfer_buffers_;

  // The last cached state received from the service.
  State last_state_;

  IPC::Channel::Sender* channel_;
  int route_id_;

  // Pending asynchronous flush callbacks.
  typedef std::queue<linked_ptr<Task> > AsyncFlushTaskQueue;
  AsyncFlushTaskQueue pending_async_flush_tasks_;

  scoped_ptr<Task> notify_repaint_task_;

  DISALLOW_COPY_AND_ASSIGN(CommandBufferProxy);
};

#endif  // ENABLE_GPU

#endif  // CHROME_RENDERER_COMMAND_BUFFER_PROXY_H_