summaryrefslogtreecommitdiffstats
path: root/components/mus/gles2/command_buffer_driver.h
blob: b65a2e22e91fed34865e14c48802108bf50cd4ed (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
// 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.

#ifndef COMPONENTS_MUS_GLES2_COMMAND_BUFFER_DRIVER_H_
#define COMPONENTS_MUS_GLES2_COMMAND_BUFFER_DRIVER_H_

#include <stdint.h>

#include "base/callback.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 "base/threading/non_thread_safe.h"
#include "base/time/time.h"
#include "gpu/command_buffer/common/capabilities.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "gpu/command_buffer/common/constants.h"
#include "mojo/public/cpp/bindings/array.h"
#include "mojo/public/cpp/system/buffer.h"
#include "ui/gfx/native_widget_types.h"
#include "ui/mojo/geometry/geometry.mojom.h"

namespace gpu {
class CommandBufferService;
class GpuScheduler;
class SyncPointClient;
class SyncPointOrderData;
namespace gles2 {
class GLES2Decoder;
}
}

namespace gfx {
class GLContext;
class GLSurface;
}

namespace mus {

class GpuState;

// This class receives CommandBuffer messages on the same thread as the native
// viewport.
class CommandBufferDriver : base::NonThreadSafe {
 public:
  class Client {
   public:
    virtual ~Client();
    virtual void DidLoseContext(uint32_t reason) = 0;
    virtual void UpdateVSyncParameters(int64_t timebase, int64_t interval) = 0;
  };
  CommandBufferDriver(gpu::CommandBufferNamespace command_buffer_namespace,
                      uint64_t command_buffer_id,
                      gfx::AcceleratedWidget widget,
                      scoped_refptr<GpuState> gpu_state);
  ~CommandBufferDriver();

  void set_client(scoped_ptr<Client> client) { client_ = std::move(client); }

  bool Initialize(mojo::ScopedSharedBufferHandle shared_state,
                  mojo::Array<int32_t> attribs);
  void SetGetBuffer(int32_t buffer);
  void Flush(int32_t put_offset);
  void RegisterTransferBuffer(int32_t id,
                              mojo::ScopedSharedBufferHandle transfer_buffer,
                              uint32_t size);
  void DestroyTransferBuffer(int32_t id);
  void CreateImage(int32_t id,
                   mojo::ScopedHandle memory_handle,
                   int32_t type,
                   mojo::SizePtr size,
                   int32_t format,
                   int32_t internal_format);
  void DestroyImage(int32_t id);
  bool IsScheduled() const;
  bool HasUnprocessedCommands() const;
  gpu::Capabilities GetCapabilities() const;
  gpu::CommandBuffer::State GetLastState() const;
  gpu::CommandBufferNamespace GetNamespaceID() const {
    return command_buffer_namespace_;
  }
  uint64_t GetCommandBufferID() const { return command_buffer_id_; }
  gpu::SyncPointOrderData* sync_point_order_data() {
    return sync_point_order_data_.get();
  }
  uint32_t GetUnprocessedOrderNum() const;
  uint32_t GetProcessedOrderNum() const;
  void SignalQuery(uint32_t query_id, const base::Closure& callback);

 private:
  bool MakeCurrent();

  // Process pending queries and call |ScheduleDelayedWork| to schedule
  // processing of delayed work.
  void ProcessPendingAndIdleWork();

  // Schedule processing of delayed work. This updates the time at which
  // delayed work should be processed. |process_delayed_work_time_| is
  // updated to current time + delay. Call this after processing some amount
  // of delayed work.
  void ScheduleDelayedWork(base::TimeDelta delay);

  // Poll the command buffer to execute work.
  void PollWork();
  void PerformWork();

  void DestroyDecoder();

  // Callbacks:
  void OnUpdateVSyncParameters(const base::TimeTicks timebase,
                               const base::TimeDelta interval);
  bool OnWaitSyncPoint(uint32_t sync_point);
  void OnFenceSyncRelease(uint64_t release);
  bool OnWaitFenceSync(gpu::CommandBufferNamespace namespace_id,
                       uint64_t command_buffer_id,
                       uint64_t release);
  void OnParseError();
  void OnContextLost(uint32_t reason);

  const gpu::CommandBufferNamespace command_buffer_namespace_;
  const uint64_t command_buffer_id_;
  gfx::AcceleratedWidget widget_;
  scoped_ptr<Client> client_;
  scoped_ptr<gpu::CommandBufferService> command_buffer_;
  scoped_ptr<gpu::gles2::GLES2Decoder> decoder_;
  scoped_ptr<gpu::GpuScheduler> scheduler_;
  scoped_refptr<gpu::SyncPointOrderData> sync_point_order_data_;
  scoped_ptr<gpu::SyncPointClient> sync_point_client_;
  scoped_refptr<gfx::GLContext> context_;
  scoped_refptr<gfx::GLSurface> surface_;
  scoped_refptr<GpuState> gpu_state_;

  scoped_refptr<base::SingleThreadTaskRunner> context_lost_task_runner_;
  base::Callback<void(int32_t)> context_lost_callback_;

  base::TimeTicks process_delayed_work_time_;
  uint32_t previous_processed_num_;
  base::TimeTicks last_idle_time_;

  base::WeakPtrFactory<CommandBufferDriver> weak_factory_;

  DISALLOW_COPY_AND_ASSIGN(CommandBufferDriver);
};

}  // namespace mus

#endif  // COMPONENTS_GLES2_COMMAND_BUFFER_DRIVER_H_