1// Copyright 2014 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 "mojo/examples/pepper_container_app/graphics_3d_resource.h"
6
7#include "base/logging.h"
8#include "mojo/examples/pepper_container_app/mojo_ppapi_globals.h"
9#include "mojo/examples/pepper_container_app/plugin_instance.h"
10#include "mojo/public/c/gles2/gles2.h"
11#include "ppapi/c/pp_errors.h"
12
13namespace mojo {
14namespace examples {
15
16namespace {
17
18gpu::CommandBuffer::State GetErrorState() {
19  gpu::CommandBuffer::State error_state;
20  error_state.error = gpu::error::kGenericError;
21  return error_state;
22}
23
24}  // namespace
25
26Graphics3DResource::Graphics3DResource(PP_Instance instance)
27    : Resource(ppapi::OBJECT_IS_IMPL, instance) {
28  ScopedMessagePipeHandle pipe = MojoPpapiGlobals::Get()->CreateGLES2Context();
29  context_ = MojoGLES2CreateContext(pipe.release().value(),
30                                    &ContextLostThunk,
31                                    &DrawAnimationFrameThunk,
32                                    this);
33}
34
35bool Graphics3DResource::IsBoundGraphics() const {
36  PluginInstance* plugin_instance =
37      MojoPpapiGlobals::Get()->GetInstance(pp_instance());
38  return plugin_instance && plugin_instance->IsBoundGraphics(pp_resource());
39}
40
41void Graphics3DResource::BindGraphics() {
42  MojoGLES2MakeCurrent(context_);
43}
44
45ppapi::thunk::PPB_Graphics3D_API* Graphics3DResource::AsPPB_Graphics3D_API() {
46  return this;
47}
48
49int32_t Graphics3DResource::GetAttribs(int32_t attrib_list[]) {
50  NOTIMPLEMENTED();
51  return PP_ERROR_FAILED;
52}
53
54int32_t Graphics3DResource::SetAttribs(const int32_t attrib_list[]) {
55  NOTIMPLEMENTED();
56  return PP_ERROR_FAILED;
57}
58
59int32_t Graphics3DResource::GetError() {
60  NOTIMPLEMENTED();
61  return PP_ERROR_FAILED;
62}
63
64int32_t Graphics3DResource::ResizeBuffers(int32_t width, int32_t height) {
65  NOTIMPLEMENTED();
66  return PP_ERROR_FAILED;
67}
68
69int32_t Graphics3DResource::SwapBuffers(
70    scoped_refptr<ppapi::TrackedCallback> callback) {
71  if (!IsBoundGraphics())
72    return PP_ERROR_FAILED;
73
74  MojoGLES2SwapBuffers();
75  return PP_OK;
76}
77
78int32_t Graphics3DResource::GetAttribMaxValue(int32_t attribute,
79                                              int32_t* value) {
80  NOTIMPLEMENTED();
81  return PP_ERROR_FAILED;
82}
83
84PP_Bool Graphics3DResource::SetGetBuffer(int32_t shm_id) {
85  NOTIMPLEMENTED();
86  return PP_FALSE;
87}
88
89scoped_refptr<gpu::Buffer> Graphics3DResource::CreateTransferBuffer(
90    uint32_t size,
91    int32* id) {
92  NOTIMPLEMENTED();
93  return NULL;
94}
95
96PP_Bool Graphics3DResource::DestroyTransferBuffer(int32_t id) {
97  NOTIMPLEMENTED();
98  return PP_FALSE;
99}
100
101PP_Bool Graphics3DResource::Flush(int32_t put_offset) {
102  NOTIMPLEMENTED();
103  return PP_FALSE;
104}
105
106gpu::CommandBuffer::State Graphics3DResource::WaitForTokenInRange(int32_t start,
107                                                                  int32_t end) {
108  NOTIMPLEMENTED();
109  return GetErrorState();
110}
111
112gpu::CommandBuffer::State Graphics3DResource::WaitForGetOffsetInRange(
113    int32_t start, int32_t end) {
114  NOTIMPLEMENTED();
115  return GetErrorState();
116}
117
118void* Graphics3DResource::MapTexSubImage2DCHROMIUM(GLenum target,
119                                                   GLint level,
120                                                   GLint xoffset,
121                                                   GLint yoffset,
122                                                   GLsizei width,
123                                                   GLsizei height,
124                                                   GLenum format,
125                                                   GLenum type,
126                                                   GLenum access) {
127  NOTIMPLEMENTED();
128  return NULL;
129}
130
131void Graphics3DResource::UnmapTexSubImage2DCHROMIUM(const void* mem) {
132  NOTIMPLEMENTED();
133}
134
135uint32_t Graphics3DResource::InsertSyncPoint() {
136  NOTIMPLEMENTED();
137  return 0;
138}
139
140Graphics3DResource::~Graphics3DResource() {
141  MojoGLES2DestroyContext(context_);
142}
143
144void Graphics3DResource::ContextLostThunk(void* closure) {
145  static_cast<Graphics3DResource*>(closure)->ContextLost();
146}
147
148void Graphics3DResource::DrawAnimationFrameThunk(void* closure) {
149  // TODO(yzshen): Use this notification to drive the SwapBuffers() callback.
150}
151
152void Graphics3DResource::ContextLost() {
153  PluginInstance* plugin_instance =
154      MojoPpapiGlobals::Get()->GetInstance(pp_instance());
155  if (plugin_instance)
156    plugin_instance->Graphics3DContextLost();
157}
158
159}  // namespace examples
160}  // namespace mojo
161