summaryrefslogtreecommitdiffstats
path: root/webkit/plugins/ppapi/ppb_context_3d_impl.cc
blob: 7d3b498b875569bc8cf974ac7ddd9806a480601b (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
379
380
381
382
383
384
385
// 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 "webkit/plugins/ppapi/common.h"
#include "webkit/plugins/ppapi/ppapi_plugin_instance.h"
#include "webkit/plugins/ppapi/ppb_surface_3d_impl.h"

namespace webkit {
namespace ppapi {

namespace {

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

bool ShmToHandle(base::SharedMemory* shm,
                 size_t size,
                 int* shm_handle,
                 uint32_t* shm_size) {
  if (!shm || !shm_handle || !shm_size)
    return 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 true;
}

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

PP_Resource Create(PP_Instance instance_id,
                   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(instance_id);
  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();
}

PP_Bool IsContext3D(PP_Resource resource) {
  return BoolToPPBool(!!Resource::GetAs<PPB_Context3D_Impl>(resource));
}

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

int32_t BindSurfaces(PP_Resource context_id,
                     PP_Resource draw,
                     PP_Resource read) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get())
    return PP_ERROR_BADRESOURCE;

  scoped_refptr<PPB_Surface3D_Impl> draw_surface(
      Resource::GetAs<PPB_Surface3D_Impl>(draw));
  if (!draw_surface.get())
    return PP_ERROR_BADRESOURCE;

  scoped_refptr<PPB_Surface3D_Impl> read_surface(
      Resource::GetAs<PPB_Surface3D_Impl>(read));
  if (!read_surface.get())
    return PP_ERROR_BADRESOURCE;

  return context->BindSurfaces(draw_surface.get(), read_surface.get());
}

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

const PPB_Context3D_Dev ppb_context3d = {
  &Create,
  &IsContext3D,
  &GetAttrib,
  &BindSurfaces,
  &GetBoundSurfaces,
};

PP_Resource CreateRaw(PP_Instance instance_id,
                      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(instance_id);
  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();
}

PP_Bool Initialize(PP_Resource context_id, int32_t size) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return PP_FALSE;
  return context->command_buffer()->Initialize(size) ? PP_TRUE : PP_FALSE;
}

PP_Bool GetRingBuffer(PP_Resource context_id,
                      int* shm_handle,
                      uint32_t* shm_size) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return PP_FALSE;

  gpu::Buffer buffer = context->command_buffer()->GetRingBuffer();

  return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size)
    ? PP_TRUE : PP_FALSE;
}

PP_Context3DTrustedState GetState(PP_Resource context_id) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer()) {
    PP_Context3DTrustedState error_state = { 0 };
    return error_state;
  }

  return PPStateFromGPUState(context->command_buffer()->GetState());
}

PP_Bool Flush(PP_Resource context_id, int32_t put_offset) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return PP_FALSE;

  context->command_buffer()->Flush(put_offset);
  return PP_TRUE;
}

PP_Context3DTrustedState FlushSync(PP_Resource context_id, int32_t put_offset) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer()) {
    PP_Context3DTrustedState error_state = { 0 };
    return error_state;
  }

  return PPStateFromGPUState(context->command_buffer()->FlushSync(put_offset));
}

int32_t CreateTransferBuffer(PP_Resource context_id, uint32_t size) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return 0;
  return context->command_buffer()->CreateTransferBuffer(size, -1);
}

PP_Bool DestroyTransferBuffer(PP_Resource context_id, int32_t id) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return PP_FALSE;
  context->command_buffer()->DestroyTransferBuffer(id);
  return PP_TRUE;
}

PP_Bool GetTransferBuffer(PP_Resource context_id,
                          int32_t id,
                          int* shm_handle,
                          uint32_t* shm_size) {
  scoped_refptr<PPB_Context3D_Impl> context(
      Resource::GetAs<PPB_Context3D_Impl>(context_id));
  if (!context.get() || !context->command_buffer())
    return PP_FALSE;
  gpu::Buffer buffer = context->command_buffer()->GetTransferBuffer(id);

  return ShmToHandle(buffer.shared_memory, buffer.size, shm_handle, shm_size)
      ? PP_TRUE : PP_FALSE;
}

}  // 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();
}

const PPB_Context3D_Dev* PPB_Context3D_Impl::GetInterface() {
  return &ppb_context3d;
}

const PPB_Context3DTrusted_Dev* PPB_Context3D_Impl::GetTrustedInterface() {
  static const PPB_Context3DTrusted_Dev iface = {
    &CreateRaw,
    &Initialize,
    &GetRingBuffer,
    &GetState,
    &Flush,
    &FlushSync,
    &CreateTransferBuffer,
    &DestroyTransferBuffer,
    &GetTransferBuffer
  };
  return &iface;
}

PPB_Context3D_Impl* PPB_Context3D_Impl::AsPPB_Context3D_Impl() {
  return this;
}

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;
  }
  if (!platform_context_->Init()) {
    Destroy();
    return false;
  }
  platform_context_->SetContextLostCallback(
      callback_factory_.NewCallback(&PPB_Context3D_Impl::OnContextLost));
  return true;
}

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::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));

  return true;
}

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

  if (draw == draw_surface_)
    return PP_OK;

  if (draw && draw->context())
    return PP_GRAPHICS3DERROR_BAD_ACCESS;

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

  draw_surface_ = draw;
  read_surface_ = read;
  return PP_OK;
}

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

  gles2_impl_.reset();

  if (command_buffer() && transfer_buffer_id_ != 0) {
    command_buffer()->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();
}

gpu::CommandBuffer *PPB_Context3D_Impl::command_buffer() {
  return platform_context_.get() ? platform_context_->GetCommandBuffer() : NULL;
}

}  // namespace ppapi
}  // namespace webkit