VR: Add Skia surface provider for command buffer

This is the last piece necessary in the VR UI to support the
command buffer.

Re-enable pixeltest for Windows.

Bug: 884256, 771794
Cq-Include-Trybots: luci.chromium.try:android_optional_gpu_tests_rel;luci.chromium.try:linux_optional_gpu_tests_rel;luci.chromium.try:linux_vr;luci.chromium.try:mac_optional_gpu_tests_rel;luci.chromium.try:win_optional_gpu_tests_rel
Change-Id: I8c0a67021c859a88b0af519ace97266a142ec7d7
Reviewed-on: https://chromium-review.googlesource.com/1238816
Commit-Queue: Aldo Culquicondor <[email protected]>
Reviewed-by: John Budorick <[email protected]>
Reviewed-by: Tibor Goldschwendt <[email protected]>
Cr-Commit-Position: refs/heads/master@{#593933}
diff --git a/chrome/browser/vr/cmd_buffer_surface_provider.cc b/chrome/browser/vr/cmd_buffer_surface_provider.cc
new file mode 100644
index 0000000..b148eaeb
--- /dev/null
+++ b/chrome/browser/vr/cmd_buffer_surface_provider.cc
@@ -0,0 +1,58 @@
+// Copyright 2018 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/browser/vr/cmd_buffer_surface_provider.h"
+
+#include "base/logging.h"
+#include "gpu/command_buffer/client/gles2_implementation.h"
+#include "gpu/command_buffer/client/gles2_lib.h"
+#include "gpu/skia_bindings/gl_bindings_skia_cmd_buffer.h"
+#include "third_party/skia/include/core/SkCanvas.h"
+#include "third_party/skia/include/core/SkSurface.h"
+#include "third_party/skia/include/gpu/GrBackendSurface.h"
+#include "third_party/skia/include/gpu/GrContext.h"
+#include "third_party/skia/include/gpu/gl/GrGLInterface.h"
+#include "ui/gfx/geometry/size.h"
+
+namespace vr {
+
+CmdBufferSurfaceProvider::CmdBufferSurfaceProvider() {
+  auto* gles2_implementation =
+      static_cast<gpu::gles2::GLES2Implementation*>(gles2::GetGLContext());
+  DCHECK(gles2_implementation);
+  sk_sp<const GrGLInterface> gr_interface =
+      skia_bindings::CreateGLES2InterfaceBindings(gles2_implementation,
+                                                  gles2_implementation);
+  gr_context_ = GrContext::MakeGL(std::move(gr_interface));
+  DCHECK(gr_context_);
+  glGetIntegerv(GL_FRAMEBUFFER_BINDING, &main_fbo_);
+}
+
+CmdBufferSurfaceProvider::~CmdBufferSurfaceProvider() = default;
+
+sk_sp<SkSurface> CmdBufferSurfaceProvider::MakeSurface(const gfx::Size& size) {
+  return SkSurface::MakeRenderTarget(
+      gr_context_.get(), SkBudgeted::kNo,
+      SkImageInfo::MakeN32Premul(size.width(), size.height()), 0,
+      kTopLeft_GrSurfaceOrigin, nullptr);
+}
+
+GLuint CmdBufferSurfaceProvider::FlushSurface(SkSurface* surface,
+                                              GLuint reuse_texture_id) {
+  surface->getCanvas()->flush();
+  GrBackendTexture backend_texture =
+      surface->getBackendTexture(SkSurface::kFlushRead_BackendHandleAccess);
+  DCHECK(backend_texture.isValid());
+  GrGLTextureInfo info;
+  bool result = backend_texture.getGLTextureInfo(&info);
+  DCHECK(result);
+  GLuint texture_id = info.fID;
+  DCHECK_NE(texture_id, 0u);
+  surface->getCanvas()->getGrContext()->resetContext();
+  glBindFramebuffer(GL_FRAMEBUFFER, main_fbo_);
+
+  return texture_id;
+}
+
+}  // namespace vr