summaryrefslogtreecommitdiffstats
path: root/ppapi/shared_impl/graphics_3d_impl.cc
blob: a01b763fe663f0b061c0d2bc00a2ec33c5e64b28 (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
// 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 "ppapi/shared_impl/graphics_3d_impl.h"

#include "base/logging.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"
#include "ppapi/c/pp_errors.h"

namespace ppapi {

Graphics3DImpl::Graphics3DImpl()
    : transfer_buffer_id_(-1),
      swap_callback_(PP_BlockUntilComplete()) {
}

Graphics3DImpl::~Graphics3DImpl() {
  // Make sure that GLES2 implementation has already been destroyed.
  DCHECK_EQ(transfer_buffer_id_, -1);
  DCHECK(!gles2_helper_.get());
  DCHECK(!gles2_impl_.get());
}

int32_t Graphics3DImpl::GetAttribs(int32_t* attrib_list) {
  // TODO(alokp): Implement me.
  return PP_ERROR_FAILED;
}

int32_t Graphics3DImpl::SetAttribs(int32_t* attrib_list) {
  // TODO(alokp): Implement me.
  return PP_ERROR_FAILED;
}

int32_t Graphics3DImpl::SwapBuffers(PP_CompletionCallback callback) {
  if (!callback.func) {
    // Blocking SwapBuffers isn't supported (since we have to be on the main
    // thread).
    return PP_ERROR_BADARGUMENT;
  }

  if (HasPendingSwap()) {
    // Already a pending SwapBuffers that hasn't returned yet.
    return PP_ERROR_INPROGRESS;
  }

  swap_callback_ = callback;
  return DoSwapBuffers();
}

void Graphics3DImpl::SwapBuffersACK(int32_t pp_error) {
  DCHECK(HasPendingSwap());
  PP_RunAndClearCompletionCallback(&swap_callback_, pp_error);
}

bool Graphics3DImpl::CreateGLES2Impl(int32 command_buffer_size,
                                     int32 transfer_buffer_size) {
  gpu::CommandBuffer* command_buffer = GetCommandBuffer();
  DCHECK(command_buffer);

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

  // Create a transfer buffer used to copy resources between the renderer
  // process and the GPU process.
  transfer_buffer_id_ =
      command_buffer->CreateTransferBuffer(transfer_buffer_size, -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(
      gles2_helper_.get(),
      transfer_buffer.size,
      transfer_buffer.ptr,
      transfer_buffer_id_,
      false));

  return true;
}

void Graphics3DImpl::DestroyGLES2Impl() {
  gles2_impl_.reset();

  if (transfer_buffer_id_ != -1) {
    gpu::CommandBuffer* command_buffer = GetCommandBuffer();
    DCHECK(command_buffer);
    command_buffer->DestroyTransferBuffer(transfer_buffer_id_);
    transfer_buffer_id_ = -1;
  }

  gles2_helper_.reset();
}

}  // namespace ppapi