Cherry-pick: Android WebView: Fall back to idle uploads if draw functor table not set

Cherry-pick with conflicts of chromium crrev.com/r286501

BUG: 16550863

Original description:

Disable map_image if draw functor table is not set,
and disallow EGL async uploads if MailboxSync is on (doesn't work).

Change-Id: I755925327b82acc7c121c8508fbc0d84229c080d
diff --git a/android_webview/browser/gpu_memory_buffer_factory_impl.cc b/android_webview/browser/gpu_memory_buffer_factory_impl.cc
index 8be58c5..398d51d 100644
--- a/android_webview/browser/gpu_memory_buffer_factory_impl.cc
+++ b/android_webview/browser/gpu_memory_buffer_factory_impl.cc
@@ -6,6 +6,7 @@
 
 #include "android_webview/public/browser/draw_gl.h"
 #include "base/logging.h"
+#include "gpu/command_buffer/service/in_process_command_buffer.h"
 #include "ui/gfx/gpu_memory_buffer.h"
 #include "ui/gfx/size.h"
 #include "ui/gl/gl_bindings.h"
@@ -92,4 +93,12 @@
   g_gl_draw_functions = table;
 }
 
+bool GpuMemoryBufferFactoryImpl::Initialize() {
+  if (!g_gl_draw_functions)
+    return false;
+
+  gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(this);
+  return true;
+}
+
 }  // namespace android_webview
diff --git a/android_webview/browser/gpu_memory_buffer_factory_impl.h b/android_webview/browser/gpu_memory_buffer_factory_impl.h
index b2c1feb..336ef40 100644
--- a/android_webview/browser/gpu_memory_buffer_factory_impl.h
+++ b/android_webview/browser/gpu_memory_buffer_factory_impl.h
@@ -19,6 +19,7 @@
   virtual ~GpuMemoryBufferFactoryImpl();
 
   static void SetAwDrawGLFunctionTable(AwDrawGLFunctionTable* table);
+  bool Initialize();
 
   // Overridden from gpu::GpuMemoryBufferFactory:
   virtual gfx::GpuMemoryBuffer* CreateGpuMemoryBuffer(
diff --git a/android_webview/lib/main/aw_main_delegate.cc b/android_webview/lib/main/aw_main_delegate.cc
index 1102806..c6b089b 100644
--- a/android_webview/lib/main/aw_main_delegate.cc
+++ b/android_webview/lib/main/aw_main_delegate.cc
@@ -24,7 +24,6 @@
 #include "content/public/browser/browser_thread.h"
 #include "content/public/common/content_switches.h"
 #include "gpu/command_buffer/client/gl_in_process_context.h"
-#include "gpu/command_buffer/service/in_process_command_buffer.h"
 #include "media/base/media_switches.h"
 #include "webkit/common/gpu/webgraphicscontext3d_in_process_command_buffer_impl.h"
 
@@ -49,14 +48,16 @@
 bool AwMainDelegate::BasicStartupComplete(int* exit_code) {
   content::SetContentClient(&content_client_);
 
-  gpu::InProcessCommandBuffer::SetGpuMemoryBufferFactory(
-      gpu_memory_buffer_factory_.get());
+  CommandLine* cl = CommandLine::ForCurrentProcess();
+  if (gpu_memory_buffer_factory_.get()->Initialize()) {
+    cl->AppendSwitch(switches::kEnableZeroCopy);
+  } else {
+    LOG(WARNING) << "Failed to initialize GpuMemoryBuffer factory";
+  }
 
   BrowserViewRenderer::CalculateTileMemoryPolicy();
 
-  CommandLine* cl = CommandLine::ForCurrentProcess();
   cl->AppendSwitch(switches::kEnableBeginFrameScheduling);
-  cl->AppendSwitch(switches::kEnableZeroCopy);
   cl->AppendSwitch(switches::kEnableImplSidePainting);
 
   // WebView uses the Android system's scrollbars and overscroll glow.
diff --git a/gpu/command_buffer/service/async_pixel_transfer_manager_android.cc b/gpu/command_buffer/service/async_pixel_transfer_manager_android.cc
index a2b2255..673cb54 100644
--- a/gpu/command_buffer/service/async_pixel_transfer_manager_android.cc
+++ b/gpu/command_buffer/service/async_pixel_transfer_manager_android.cc
@@ -10,6 +10,7 @@
 #include "gpu/command_buffer/service/async_pixel_transfer_manager_idle.h"
 #include "gpu/command_buffer/service/async_pixel_transfer_manager_stub.h"
 #include "gpu/command_buffer/service/async_pixel_transfer_manager_sync.h"
+#include "gpu/command_buffer/service/mailbox_synchronizer.h"
 #include "ui/gl/gl_context.h"
 #include "ui/gl/gl_implementation.h"
 
@@ -63,7 +64,8 @@
           !IsBroadcom() &&
           !IsImagination() &&
           !IsNvidia31() &&
-          !base::android::SysUtils::IsLowEndDevice()) {
+          !base::android::SysUtils::IsLowEndDevice() &&
+          !gles2::MailboxSynchronizer::GetInstance()) {
         return new AsyncPixelTransferManagerEGL;
       }
       return new AsyncPixelTransferManagerIdle;