summaryrefslogtreecommitdiffstats
path: root/mojo/gles2/gles2_support_impl.cc
blob: 888cfdec9e96c039db7c0e973a97418825696961 (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
// 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.

#include "mojo/gles2/gles2_support_impl.h"

#include "base/lazy_instance.h"
#include "gpu/command_buffer/client/gles2_interface.h"
#include "mojo/gles2/gles2_context.h"
#include "mojo/public/gles2/gles2_interface.h"
#include "mojo/public/gles2/gles2_private.h"

namespace mojo {
namespace gles2 {

namespace {

class GLES2ImplForCommandBuffer : public GLES2Interface {
 public:
  GLES2ImplForCommandBuffer() : gpu_interface_(NULL) {}

  void set_gpu_interface(gpu::gles2::GLES2Interface* gpu_interface) {
    gpu_interface_ = gpu_interface;
  }
  gpu::gles2::GLES2Interface* gpu_interface() const { return gpu_interface_; }

#define VISIT_GL_CALL(Function, ReturnType, PARAMETERS, ARGUMENTS) \
  virtual ReturnType Function PARAMETERS OVERRIDE {                \
    return gpu_interface_->Function ARGUMENTS;                     \
  }
#include "mojo/public/c/gles2/gles2_call_visitor_autogen.h"
#undef VISIT_GL_CALL

 private:
  gpu::gles2::GLES2Interface* gpu_interface_;
  DISALLOW_COPY_AND_ASSIGN(GLES2ImplForCommandBuffer);
};

base::LazyInstance<GLES2ImplForCommandBuffer> g_gles2_interface =
    LAZY_INSTANCE_INITIALIZER;

}  // anonymous namespace

GLES2SupportImpl::GLES2SupportImpl() : async_waiter_(NULL) {}
GLES2SupportImpl::~GLES2SupportImpl() {}

// static
void GLES2SupportImpl::Init() { GLES2Support::Init(new GLES2SupportImpl()); }

void GLES2SupportImpl::Initialize(MojoAsyncWaiter* async_waiter) {
  DCHECK(!async_waiter_);
  DCHECK(async_waiter);
  async_waiter_ = async_waiter;
}

void GLES2SupportImpl::Terminate() {
  DCHECK(async_waiter_);
  async_waiter_ = NULL;
}

MojoGLES2Context GLES2SupportImpl::CreateContext(
    MessagePipeHandle handle,
    MojoGLES2ContextLost lost_callback,
    MojoGLES2DrawAnimationFrame animation_callback,
    void* closure) {
  ScopedCommandBufferHandle scoped_handle(CommandBufferHandle(handle.value()));
  scoped_ptr<GLES2Context> client(new GLES2Context(async_waiter_,
                                                   scoped_handle.Pass(),
                                                   lost_callback,
                                                   animation_callback,
                                                   closure));
  if (!client->Initialize())
    client.reset();
  return client.release();
}

void GLES2SupportImpl::DestroyContext(MojoGLES2Context context) {
  delete static_cast<GLES2Context*>(context);
}

void GLES2SupportImpl::MakeCurrent(MojoGLES2Context context) {
  gpu::gles2::GLES2Interface* interface = NULL;
  if (context) {
    GLES2Context* client = static_cast<GLES2Context*>(context);
    interface = client->interface();
    DCHECK(interface);
  }
  g_gles2_interface.Get().set_gpu_interface(interface);
}

void GLES2SupportImpl::SwapBuffers() {
  g_gles2_interface.Get().gpu_interface()->SwapBuffers();
}

void GLES2SupportImpl::RequestAnimationFrames(MojoGLES2Context context) {
  static_cast<GLES2Context*>(context)->RequestAnimationFrames();
}

void GLES2SupportImpl::CancelAnimationFrames(MojoGLES2Context context) {
  static_cast<GLES2Context*>(context)->CancelAnimationFrames();
}

void* GLES2SupportImpl::GetGLES2Interface(MojoGLES2Context context) {
  return static_cast<GLES2Context*>(context)->interface();
}

void* GLES2SupportImpl::GetContextSupport(MojoGLES2Context context) {
  return static_cast<GLES2Context*>(context)->context_support();
}

GLES2Interface* GLES2SupportImpl::GetGLES2InterfaceForCurrentContext() {
  return &g_gles2_interface.Get();
}

}  // namespace gles2
}  // namespace mojo