summaryrefslogtreecommitdiffstats
path: root/content/renderer/pepper_platform_context_3d_impl.cc
blob: af08efd90227494c95083d2eff975251ad564474 (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
// 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 "content/renderer/pepper_platform_context_3d_impl.h"

#include "base/bind.h"
#include "content/renderer/render_thread_impl.h"
#include "content/renderer/gpu/renderer_gl_context.h"
#include "content/renderer/gpu/gpu_channel_host.h"
#include "content/renderer/gpu/command_buffer_proxy.h"
#include "googleurl/src/gurl.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"

#ifdef ENABLE_GPU

PlatformContext3DImpl::PlatformContext3DImpl(RendererGLContext* parent_context)
      : parent_context_(parent_context->AsWeakPtr()),
        parent_texture_id_(0),
        command_buffer_(NULL),
        weak_ptr_factory_(ALLOW_THIS_IN_INITIALIZER_LIST(this)) {
}

PlatformContext3DImpl::~PlatformContext3DImpl() {
  if (parent_context_.get() && parent_texture_id_ != 0) {
    // Flush any remaining commands in the parent context to make sure the
    // texture id accounting stays consistent.
    gpu::gles2::GLES2Implementation* parent_gles2 =
        parent_context_->GetImplementation();
    parent_gles2->helper()->CommandBufferHelper::Finish();
    parent_gles2->FreeTextureId(parent_texture_id_);
  }

  if (command_buffer_) {
    DCHECK(channel_.get());
    channel_->DestroyCommandBuffer(command_buffer_);
    command_buffer_ = NULL;
  }

  channel_ = NULL;
}

bool PlatformContext3DImpl::Init(const int32* attrib_list) {
  // Ignore initializing more than once.
  if (command_buffer_)
    return true;

  // Parent may already have been deleted.
  if (!parent_context_.get())
    return false;

  RenderThreadImpl* render_thread = RenderThreadImpl::current();
  if (!render_thread)
    return false;

  channel_ = render_thread->GetGpuChannel();
  if (!channel_.get())
    return false;

  DCHECK(channel_->state() == GpuChannelHost::kConnected);

  // Flush any remaining commands in the parent context to make sure the
  // texture id accounting stays consistent.
  gpu::gles2::GLES2Implementation* parent_gles2 =
      parent_context_->GetImplementation();
  parent_gles2->helper()->CommandBufferHelper::Finish();
  parent_texture_id_ = parent_gles2->MakeTextureId();

  gfx::Size surface_size;
  std::vector<int32> attribs;
  // TODO(alokp): Change GpuChannelHost::CreateOffscreenCommandBuffer()
  // interface to accept width and height in the attrib_list so that
  // we do not need to filter for width and height here.
  if (attrib_list) {
    for (const int32_t* attr = attrib_list;
         attr[0] != RendererGLContext::NONE;
         attr += 2) {
      switch (attr[0]) {
        case RendererGLContext::WIDTH:
          surface_size.set_width(attr[1]);
          break;
        case RendererGLContext::HEIGHT:
          surface_size.set_height(attr[1]);
          break;
        default:
          attribs.push_back(attr[0]);
          attribs.push_back(attr[1]);
          break;
      }
    }
    attribs.push_back(RendererGLContext::NONE);
  }

  CommandBufferProxy* parent_command_buffer =
      parent_context_->GetCommandBufferProxy();
  command_buffer_ = channel_->CreateOffscreenCommandBuffer(
      surface_size,
      NULL,
      "*",
      attribs,
      GURL::EmptyGURL());
  if (!command_buffer_)
    return false;

  if (!command_buffer_->SetParent(parent_command_buffer, parent_texture_id_))
    return false;

  command_buffer_->SetChannelErrorCallback(
      base::Bind(&PlatformContext3DImpl::OnContextLost,
                 weak_ptr_factory_.GetWeakPtr()));

  return true;
}

unsigned PlatformContext3DImpl::GetBackingTextureId() {
  DCHECK(command_buffer_);
  return parent_texture_id_;
}

gpu::CommandBuffer* PlatformContext3DImpl::GetCommandBuffer() {
  return command_buffer_;
}

int PlatformContext3DImpl::GetCommandBufferRouteId() {
  DCHECK(command_buffer_);
  return command_buffer_->route_id();
}

void PlatformContext3DImpl::SetContextLostCallback(const base::Closure& task) {
  context_lost_callback_ = task;
}

bool PlatformContext3DImpl::Echo(const base::Closure& task) {
  return command_buffer_->Echo(task);
}

void PlatformContext3DImpl::OnContextLost() {
  DCHECK(command_buffer_);

  if (!context_lost_callback_.is_null())
    context_lost_callback_.Run();
}

#endif  // ENABLE_GPU