<commit_before>// Copyright (c) 2010 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 "chrome/renderer/pepper_platform_context_3d_impl.h"

#include "chrome/renderer/command_buffer_proxy.h"
#include "chrome/renderer/ggl/ggl.h"
#include "chrome/renderer/gpu_channel_host.h"
#include "chrome/renderer/render_thread.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"

#ifdef ENABLE_GPU
PlatformContext3DImpl::PlatformContext3DImpl(ggl::Context* parent_context)
      : parent_context_(parent_context),
        command_buffer_(NULL) {
}

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

  channel_ = NULL;

  if (parent_context_ && parent_texture_id_ != 0) {
    ggl::GetImplementation(parent_context_)->FreeTextureId(parent_texture_id_);
  }

}

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

  RenderThread* render_thread = RenderThread::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 =
      ggl::GetImplementation(parent_context_);
  parent_gles2->helper()->CommandBufferHelper::Finish();
  parent_texture_id_ = parent_gles2->MakeTextureId();

  // TODO(apatrick): Let Pepper plugins configure their back buffer surface.
  static const int32 kAttribs[] = {
    ggl::GGL_ALPHA_SIZE, 8,
    ggl::GGL_DEPTH_SIZE, 24,
    ggl::GGL_STENCIL_SIZE, 8,
    ggl::GGL_SAMPLES, 0,
    ggl::GGL_SAMPLE_BUFFERS, 0,
    ggl::GGL_NONE,
  };
  std::vector<int32> attribs(kAttribs, kAttribs + ARRAYSIZE_UNSAFE(kAttribs));
  CommandBufferProxy* parent_command_buffer =
      ggl::GetCommandBufferProxy(parent_context_);
  command_buffer_ = channel_->CreateOffscreenCommandBuffer(
        parent_command_buffer,
        gfx::Size(1, 1),
        "",
        attribs,
        parent_texture_id_);

  if (!command_buffer_)
    return false;

  return true;
}

void PlatformContext3DImpl::SetSwapBuffersCallback(Callback0::Type* callback) {
  DCHECK(command_buffer_);
  command_buffer_->SetSwapBuffersCallback(callback);
}

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

gpu::CommandBuffer*
    PlatformContext3DImpl::GetCommandBuffer() {
  return command_buffer_;<commit_msg>Allow GL extensions in PPAPI.<commit_after>// Copyright (c) 2010 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 "chrome/renderer/pepper_platform_context_3d_impl.h"

#include "chrome/renderer/command_buffer_proxy.h"
#include "chrome/renderer/ggl/ggl.h"
#include "chrome/renderer/gpu_channel_host.h"
#include "chrome/renderer/render_thread.h"
#include "gpu/command_buffer/client/gles2_cmd_helper.h"
#include "gpu/command_buffer/client/gles2_implementation.h"

#ifdef ENABLE_GPU
PlatformContext3DImpl::PlatformContext3DImpl(ggl::Context* parent_context)
      : parent_context_(parent_context),
        command_buffer_(NULL) {
}

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

  channel_ = NULL;

  if (parent_context_ && parent_texture_id_ != 0) {
    ggl::GetImplementation(parent_context_)->FreeTextureId(parent_texture_id_);
  }

}

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

  RenderThread* render_thread = RenderThread::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 =
      ggl::GetImplementation(parent_context_);
  parent_gles2->helper()->CommandBufferHelper::Finish();
  parent_texture_id_ = parent_gles2->MakeTextureId();

  // TODO(apatrick): Let Pepper plugins configure their back buffer surface.
  static const int32 kAttribs[] = {
    ggl::GGL_ALPHA_SIZE, 8,
    ggl::GGL_DEPTH_SIZE, 24,
    ggl::GGL_STENCIL_SIZE, 8,
    ggl::GGL_SAMPLES, 0,
    ggl::GGL_SAMPLE_BUFFERS, 0,
    ggl::GGL_NONE,
  };
  std::vector<int32> attribs(kAttribs, kAttribs + ARRAYSIZE_UNSAFE(kAttribs));
  CommandBufferProxy* parent_command_buffer =
      ggl::GetCommandBufferProxy(parent_context_);
  command_buffer_ = channel_->CreateOffscreenCommandBuffer(
        parent_command_buffer,
        gfx::Size(1, 1),
        "*",
        attribs,
        parent_texture_id_);

  if (!command_buffer_)
    return false;

  return true;
}

void PlatformContext3DImpl::SetSwapBuffersCallback(Callback0::Type* callback) {
  DCHECK(command_buffer_);
  command_buffer_->SetSwapBuffersCallback(callback);
}

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

gpu::CommandBuffer*
    PlatformContext3DImpl::GetCommandBuffer() {
  return command_buffer_;
{'fraction_non_alphanumeric': 0.10975824951473442, 'fraction_numerical': 0.011999294159167107, 'mean_word_length': 5.029787234042553, 'pattern_counts': {'":': 0, '<': 5, '<?xml version=': 0, '>': 23, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '4257895', 'n_tokens_mistral': 1861, 'n_tokens_neox': 1844, 'n_words': 343}