summaryrefslogtreecommitdiffstats
path: root/components/mus/gles2/command_buffer_impl.h
blob: cbe91cfcfc59eff4b637647789db627d279aed7e (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
// Copyright 2014 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 COMPONENTS_MUS_GLES2_COMMAND_BUFFER_IMPL_H_
#define COMPONENTS_MUS_GLES2_COMMAND_BUFFER_IMPL_H_

#include <stdint.h>

#include "base/macros.h"
#include "base/memory/scoped_ptr.h"
#include "base/memory/weak_ptr.h"
#include "base/single_thread_task_runner.h"
#include "components/mus/public/interfaces/command_buffer.mojom.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "mojo/public/cpp/bindings/binding.h"

namespace mus {

class CommandBufferDriver;
class CommandBufferImplObserver;
class GpuState;

// This class listens to the CommandBuffer message pipe on a low-latency thread
// so that we can insert sync points without blocking on the GL driver. It
// forwards most method calls to the CommandBufferDriver, which runs on the
// same thread as the native viewport.
class CommandBufferImpl : public mojom::CommandBuffer {
 public:
  CommandBufferImpl(mojo::InterfaceRequest<CommandBuffer> request,
                    scoped_refptr<GpuState> gpu_state);
  void DidLoseContext(uint32_t reason);

  void set_observer(CommandBufferImplObserver* observer) {
    observer_ = observer;
  }

 private:
  class CommandBufferDriverClientImpl;
  ~CommandBufferImpl() override;

  // mojom::CommandBuffer:
  void Initialize(
      mojom::CommandBufferLostContextObserverPtr loss_observer,
      mojo::ScopedSharedBufferHandle shared_state,
      mojo::Array<int32_t> attribs,
      const mojom::CommandBuffer::InitializeCallback& callback) override;
  void SetGetBuffer(int32_t buffer) override;
  void Flush(int32_t put_offset) override;
  void MakeProgress(
      int32_t last_get_offset,
      const mojom::CommandBuffer::MakeProgressCallback& callback) override;
  void RegisterTransferBuffer(int32_t id,
                              mojo::ScopedSharedBufferHandle transfer_buffer,
                              uint32_t size) override;
  void DestroyTransferBuffer(int32_t id) override;
  void CreateImage(int32_t id,
                   mojo::ScopedHandle memory_handle,
                   int32_t type,
                   mojo::SizePtr size,
                   int32_t format,
                   int32_t internal_format) override;
  void DestroyImage(int32_t id) override;

  // All helper functions are called in the GPU therad.
  void InitializeOnGpuThread(
      mojom::CommandBufferLostContextObserverPtr loss_observer,
      mojo::ScopedSharedBufferHandle shared_state,
      mojo::Array<int32_t> attribs,
      const base::Callback<void(mojom::CommandBufferInfoPtr)>& callback);
  bool SetGetBufferOnGpuThread(int32_t buffer);
  bool FlushOnGpuThread(int32_t put_offset, uint32_t order_num);
  bool MakeProgressOnGpuThread(
      int32_t last_get_offset,
      const base::Callback<void(const gpu::CommandBuffer::State&)>& callback);
  bool RegisterTransferBufferOnGpuThread(
      int32_t id,
      mojo::ScopedSharedBufferHandle transfer_buffer,
      uint32_t size);
  bool DestroyTransferBufferOnGpuThread(int32_t id);
  bool CreateImageOnGpuThread(int32_t id,
                              mojo::ScopedHandle memory_handle,
                              int32_t type,
                              mojo::SizePtr size,
                              int32_t format,
                              int32_t internal_format);
  bool DestroyImageOnGpuThread(int32_t id);

  void BindToRequest(mojo::InterfaceRequest<CommandBuffer> request);

  void OnConnectionError();
  bool DeleteOnGpuThread();

  scoped_refptr<GpuState> gpu_state_;
  scoped_ptr<CommandBufferDriver> driver_;
  scoped_ptr<mojo::Binding<CommandBuffer>> binding_;
  CommandBufferImplObserver* observer_;
  mojom::CommandBufferLostContextObserverPtr loss_observer_;

  DISALLOW_COPY_AND_ASSIGN(CommandBufferImpl);
};

}  // namespace mus

#endif  // COMPONENTS_MUS_GLES2_COMMAND_BUFFER_IMPL_H_