summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_context_3d_impl.cc
blob: 3aa2037dbf0313f06a8c6d6b93afbe8118ac702b (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
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
// Copyright (c) 2011 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 "webkit/plugins/ppapi/ppb_context_3d_impl.h"

#include "base/logging.h"
#include "base/shared_memory.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "gpu/command_buffer/common/command_buffer.h"
#include "ppapi/c/dev/ppb_context_3d_trusted_dev.h"
#include "ppapi/thunk/enter.h"
#include "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_surface_3d_impl.h"

using ppapi::thunk::EnterResourceNoLock;
using ppapi::thunk::PPB_Context3D_API;
using ppapi::thunk::PPB_Surface3D_API;

namespace webkit {
namespace ppapi {

namespace {

// Size of the transfer buffer.
const int32 kCommandBufferSize = 1024 * 1024;
const int32 kTransferBufferSize = 1024 * 1024;

PP_Bool ShmToHandle(base::SharedMemory* shm,
                    size_t size,
                    int* shm_handle,
                    uint32_t* shm_size) {
  if (!shm || !shm_handle || !shm_size)
    return PP_FALSE;
#if defined(OS_POSIX)
  *shm_handle = shm->handle().fd;
#elif defined(OS_WIN)
  *shm_handle = reinterpret_cast<int>(shm->handle());
#else
  #error "Platform not supported."
#endif
  *shm_size = size;
  return PP_TRUE;
}

PP_Context3DTrustedState GetErrorState() {
  PP_Context3DTrustedState error_state = { 0 };
  error_state.error = kGenericError;
  return error_state;
}

PP_Context3DTrustedState PPStateFromGPUState(
    const gpu::CommandBuffer::State& s) {
  PP_Context3DTrustedState state = {
      s.num_entries,
      s.get_offset,
      s.put_offset,
      s.token,
      static_cast<PPB_Context3DTrustedError>(s.error),
      s.generation
  };
  return state;
}

}  // namespace

PPB_Context3D_Impl::PPB_Context3D_Impl(PluginInstance* instance)
    : Resource(instance),
      instance_(instance),
      transfer_buffer_id_(0),
      draw_surface_(NULL),
      read_surface_(NULL),
      callback_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}

PPB_Context3D_Impl::~PPB_Context3D_Impl() {
  Destroy();
}

// static
PP_Resource PPB_Context3D_Impl::Create(PP_Instance pp_instance,
                                       PP_Config3D_Dev config,
                                       PP_Resource share_context,
                                       const int32_t* attrib_list) {
  // TODO(alokp): Support shared context.
  DCHECK_EQ(0, share_context);
  if (share_context != 0)
    return 0;

  PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance);
  if (!instance)
    return 0;

  scoped_refptr<PPB_Context3D_Impl> context(
      new PPB_Context3D_Impl(instance));
  if (!context->Init(config, share_context, attrib_list))
    return 0;

  return context->GetReference();
}

// static
PP_Resource PPB_Context3D_Impl::CreateRaw(PP_Instance pp_instance,
                                          PP_Config3D_Dev config,
                                          PP_Resource share_context,
                                          const int32_t* attrib_list) {
  // TODO(alokp): Support shared context.
  DCHECK_EQ(0, share_context);
  if (share_context != 0)
    return 0;

  PluginInstance* instance = ResourceTracker::Get()->GetInstance(pp_instance);
  if (!instance)
    return 0;

  scoped_refptr<PPB_Context3D_Impl> context(
      new PPB_Context3D_Impl(instance));
  if (!context->InitRaw(config, share_context, attrib_list))
    return 0;

  return context->GetReference();
}

PPB_Context3D_API* PPB_Context3D_Impl::AsPPB_Context3D_API() {
  return this;
}

int32_t PPB_Context3D_Impl::GetAttrib(int32_t attribute, int32_t* value) {
  // TODO(alokp): Implement me.
  return 0;
}

int32_t PPB_Context3D_Impl::BindSurfaces(PP_Resource draw, PP_Resource read) {
  EnterResourceNoLock<PPB_Surface3D_API> enter_draw(draw, true);
  if (enter_draw.failed())
    return PP_ERROR_BADRESOURCE;
  PPB_Surface3D_Impl* new_draw =
      static_cast<PPB_Surface3D_Impl*>(enter_draw.object());

  EnterResourceNoLock<PPB_Surface3D_API> enter_read(read, true);
  if (enter_read.failed())
    return PP_ERROR_BADRESOURCE;
  PPB_Surface3D_Impl* new_read =
      static_cast<PPB_Surface3D_Impl*>(enter_read.object());
  return BindSurfacesImpl(new_draw, new_read);
}

int32_t PPB_Context3D_Impl::BindSurfacesImpl(PPB_Surface3D_Impl* new_draw,
                                             PPB_Surface3D_Impl* new_read) {
  // TODO(alokp): Support separate draw-read surfaces.
  DCHECK_EQ(new_draw, new_read);
  if (new_draw != new_read)
    return PP_ERROR_NOTSUPPORTED;

  if (new_draw == draw_surface_)
    return PP_OK;

  if (new_draw && new_draw->context())
    return PP_ERROR_BADARGUMENT;  // Already bound.

  if (draw_surface_)
    draw_surface_->BindToContext(NULL);
  if (new_draw && !new_draw->BindToContext(this))
    return PP_ERROR_NOMEMORY;

  draw_surface_ = new_draw;
  read_surface_ = new_read;
  return PP_OK;
}

int32_t PPB_Context3D_Impl::GetBoundSurfaces(PP_Resource* draw,
                                             PP_Resource* read) {
  // TODO(alokp): Implement me.
  return 0;
}

PP_Bool PPB_Context3D_Impl::InitializeTrusted(int32_t size) {
  if (!platform_context_.get())
    return PP_FALSE;
  return PP_FromBool(platform_context_->GetCommandBuffer()->Initialize(size));
}

PP_Bool PPB_Context3D_Impl::GetRingBuffer(int* shm_handle,
                                          uint32_t* shm_size) {
  if (!platform_context_.get())
    return PP_FALSE;
  gpu::Buffer buffer = platform_context_->GetCommandBuffer()->GetRingBuffer();
  return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size);
}

PP_Context3DTrustedState PPB_Context3D_Impl::GetState() {
  if (!platform_context_.get())
    return GetErrorState();
  return PPStateFromGPUState(platform_context_->GetCommandBuffer()->GetState());
}

PP_Bool PPB_Context3D_Impl::Flush(int32_t put_offset) {
  if (!platform_context_.get())
    return PP_FALSE;
  platform_context_->GetCommandBuffer()->Flush(put_offset);
  return PP_TRUE;
}

PP_Context3DTrustedState PPB_Context3D_Impl::FlushSync(int32_t put_offset) {
  if (!platform_context_.get())
    return GetErrorState();
  gpu::CommandBuffer::State state =
      platform_context_->GetCommandBuffer()->GetState();
  return PPStateFromGPUState(
      platform_context_->GetCommandBuffer()->FlushSync(put_offset,
                                                       state.get_offset));
}

int32_t PPB_Context3D_Impl::CreateTransferBuffer(uint32_t size) {
  if (!platform_context_.get())
    return 0;
  return platform_context_->GetCommandBuffer()->CreateTransferBuffer(size, -1);
}

PP_Bool PPB_Context3D_Impl::DestroyTransferBuffer(int32_t id) {
  if (!platform_context_.get())
    return PP_FALSE;
  platform_context_->GetCommandBuffer()->DestroyTransferBuffer(id);
  return PP_TRUE;
}

PP_Bool PPB_Context3D_Impl::GetTransferBuffer(int32_t id,
                                              int* shm_handle,
                                              uint32_t* shm_size) {
  if (!platform_context_.get())
    return PP_FALSE;
  gpu::Buffer buffer =
      platform_context_->GetCommandBuffer()->GetTransferBuffer(id);
  return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size);
}

PP_Context3DTrustedState PPB_Context3D_Impl::FlushSyncFast(
    int32_t put_offset,
    int32_t last_known_get) {
  if (!platform_context_.get())
    return GetErrorState();
  return PPStateFromGPUState(
      platform_context_->GetCommandBuffer()->FlushSync(put_offset,
                                                       last_known_get));
}

void* PPB_Context3D_Impl::MapTexSubImage2DCHROMIUM(GLenum target,
                                                   GLint level,
                                                   GLint xoffset,
                                                   GLint yoffset,
                                                   GLsizei width,
                                                   GLsizei height,
                                                   GLenum format,
                                                   GLenum type,
                                                   GLenum access) {
  if (!gles2_impl_.get())
    return NULL;
  return gles2_impl_->MapTexSubImage2DCHROMIUM(
      target, level, xoffset, yoffset, width, height, format, type, access);
}

void PPB_Context3D_Impl::UnmapTexSubImage2DCHROMIUM(const void* mem) {
  if (gles2_impl_.get())
    gles2_impl_->UnmapTexSubImage2DCHROMIUM(mem);
}

gpu::gles2::GLES2Implementation* PPB_Context3D_Impl::GetGLES2Impl() {
  return gles2_impl();
}

bool PPB_Context3D_Impl::Init(PP_Config3D_Dev config,
                              PP_Resource share_context,
                              const int32_t* attrib_list) {
  if (!InitRaw(config, share_context, attrib_list))
    return false;

  if (!CreateImplementation()) {
    Destroy();
    return false;
  }

  return true;
}

bool PPB_Context3D_Impl::InitRaw(PP_Config3D_Dev config,
                                 PP_Resource share_context,
                                 const int32_t* attrib_list) {
  // Create and initialize the objects required to issue GLES2 calls.
  platform_context_.reset(instance()->CreateContext3D());
  if (!platform_context_.get()) {
    Destroy();
    return false;
  }

  static const int32 kAttribs[] = {
    PP_GRAPHICS3DATTRIB_ALPHA_SIZE, 8,
    PP_GRAPHICS3DATTRIB_DEPTH_SIZE, 24,
    PP_GRAPHICS3DATTRIB_STENCIL_SIZE, 8,
    PP_GRAPHICS3DATTRIB_SAMPLES, 0,
    PP_GRAPHICS3DATTRIB_SAMPLE_BUFFERS, 0,
    PP_GRAPHICS3DATTRIB_HEIGHT, 1,
    PP_GRAPHICS3DATTRIB_WIDTH, 1,
    PP_GRAPHICS3DATTRIB_NONE,
  };
  if (!platform_context_->Init(kAttribs)) {
    Destroy();
    return false;
  }

  platform_context_->SetContextLostCallback(
      callback_factory_.NewCallback(&PPB_Context3D_Impl::OnContextLost));
  return true;
}

bool PPB_Context3D_Impl::CreateImplementation() {
  gpu::CommandBuffer* command_buffer = platform_context_->GetCommandBuffer();
  DCHECK(command_buffer);

  if (!command_buffer->Initialize(kCommandBufferSize))
    return false;

  // Create the GLES2 helper, which writes the command buffer protocol.
  helper_.reset(new gpu::gles2::GLES2CmdHelper(command_buffer));
  if (!helper_->Initialize(kCommandBufferSize))
    return false;

  // Create a transfer buffer used to copy resources between the renderer
  // process and the GPU process.
  transfer_buffer_id_ =
      command_buffer->CreateTransferBuffer(kTransferBufferSize, -1);
  if (transfer_buffer_id_ < 0)
    return false;

  // Map the buffer into the renderer process's address space.
  gpu::Buffer transfer_buffer =
      command_buffer->GetTransferBuffer(transfer_buffer_id_);
  if (!transfer_buffer.ptr)
    return false;

  // Create the object exposing the OpenGL API.
  gles2_impl_.reset(new gpu::gles2::GLES2Implementation(
      helper_.get(),
      transfer_buffer.size,
      transfer_buffer.ptr,
      transfer_buffer_id_,
      false,
      true));

  return true;
}

void PPB_Context3D_Impl::Destroy() {
  if (draw_surface_)
    draw_surface_->BindToContext(NULL);

  gles2_impl_.reset();

  if (platform_context_.get() && transfer_buffer_id_ != 0) {
    platform_context_->GetCommandBuffer()->DestroyTransferBuffer(
        transfer_buffer_id_);
    transfer_buffer_id_ = 0;
  }

  helper_.reset();
  platform_context_.reset();
}

void PPB_Context3D_Impl::OnContextLost() {
  if (draw_surface_)
    draw_surface_->OnContextLost();
  if (read_surface_)
    read_surface_->OnContextLost();
}

}  // namespace ppapi
}  // namespace webkit