Update from https://crrev.com/316786

List of manually-modified files:
gpu/command_buffer/service/in_process_command_buffer.cc
examples/sample_app/BUILD.gn
examples/sample_app/spinning_cube.cc
mojo/android/javatests/src/org/chromium/mojo/MojoTestCase.java
mojo/cc/context_provider_mojo.cc
mojo/cc/context_provider_mojo.h
mojo/common/trace_controller_impl.cc
mojo/gles2/command_buffer_client_impl.cc
mojo/gles2/command_buffer_client_impl.h
services/gles2/gpu_impl.cc
shell/android/apk/src/org/chromium/mojo/shell/MojoShellApplication.java
sky/engine/core/dom/Node.cpp
sky/shell/apk/src/org/domokit/sky/shell/SkyShellApplication.java
ui/events/latency_info.cc
ui/gfx/transform.cc
ui/gfx/transform.h
ui/gfx/transform_util.cc
ui/gfx/transform_util.h

Review URL: https://codereview.chromium.org/935333002
diff --git a/ui/events/latency_info.cc b/ui/events/latency_info.cc
index 145b77a..9d16b29 100644
--- a/ui/events/latency_info.cc
+++ b/ui/events/latency_info.cc
@@ -71,7 +71,7 @@
 }
 
 // This class is for converting latency info to trace buffer friendly format.
-class LatencyInfoTracedValue : public base::debug::ConvertableToTraceFormat {
+class LatencyInfoTracedValue : public base::trace_event::ConvertableToTraceFormat {
  public:
   static scoped_refptr<ConvertableToTraceFormat> FromValue(
       scoped_ptr<base::Value> value);
@@ -87,9 +87,9 @@
   DISALLOW_COPY_AND_ASSIGN(LatencyInfoTracedValue);
 };
 
-scoped_refptr<base::debug::ConvertableToTraceFormat>
+scoped_refptr<base::trace_event::ConvertableToTraceFormat>
 LatencyInfoTracedValue::FromValue(scoped_ptr<base::Value> value) {
-  return scoped_refptr<base::debug::ConvertableToTraceFormat>(
+  return scoped_refptr<base::trace_event::ConvertableToTraceFormat>(
       new LatencyInfoTracedValue(value.release()));
 }
 
@@ -107,7 +107,7 @@
 }
 
 // Converts latencyinfo into format that can be dumped into trace buffer.
-scoped_refptr<base::debug::ConvertableToTraceFormat> AsTraceableData(
+scoped_refptr<base::trace_event::ConvertableToTraceFormat> AsTraceableData(
     const ui::LatencyInfo& latency) {
   scoped_ptr<base::DictionaryValue> record_data(new base::DictionaryValue());
   for (ui::LatencyInfo::LatencyMap::const_iterator it =
diff --git a/ui/gfx/transform.cc b/ui/gfx/transform.cc
index 7fe5174..c13ae1d 100644
--- a/ui/gfx/transform.cc
+++ b/ui/gfx/transform.cc
@@ -40,6 +40,12 @@
   return std::abs(x - SkDoubleToMScalar(1.0)) <= tolerance;
 }
 
+static float Round(float f) {
+  if (f == 0.f)
+    return f;
+  return (f > 0.f) ? std::floor(f + 0.5f) : std::ceil(f - 0.5f);
+}
+
 }  // namespace
 
 Transform::Transform(SkMScalar col1row1,
@@ -493,6 +499,12 @@
   return true;
 }
 
+void Transform::RoundTranslationComponents() {
+  matrix_.set(0, 3, Round(matrix_.get(0, 3)));
+  matrix_.set(1, 3, Round(matrix_.get(1, 3)));
+}
+
+
 void Transform::TransformPointInternal(const SkMatrix44& xform,
                                        Point3F* point) const {
   if (xform.isIdentity())
diff --git a/ui/gfx/transform.h b/ui/gfx/transform.h
index 8156b72..68a734e 100644
--- a/ui/gfx/transform.h
+++ b/ui/gfx/transform.h
@@ -238,6 +238,8 @@
   // DecomposedTransform.
   bool Blend(const Transform& from, double progress);
 
+  void RoundTranslationComponents();
+
   // Returns |this| * |other|.
   Transform operator*(const Transform& other) const {
     return Transform(*this, other);
diff --git a/ui/gfx/transform_util.cc b/ui/gfx/transform_util.cc
index 655ce57..e96d96e 100644
--- a/ui/gfx/transform_util.cc
+++ b/ui/gfx/transform_util.cc
@@ -498,4 +498,21 @@
       quaternion[3]);
 }
 
+float MatrixDistance(const Transform& a, const Transform& b) {
+  double sum = 0.0;
+
+  const SkMatrix44& a_data = a.matrix();
+  const SkMatrix44& b_data = b.matrix();
+
+  for (int row = 0; row < 4; ++row) {
+    for (int col = 0; col < 4; ++col) {
+      double diff = a_data.get(row, col) - b_data.get(row, col);
+      sum += diff * diff;
+    }
+  }
+
+  return static_cast<float>(std::sqrt(sum));
+}
+
+
 }  // namespace ui
diff --git a/ui/gfx/transform_util.h b/ui/gfx/transform_util.h
index a77ded2..6b091a2 100644
--- a/ui/gfx/transform_util.h
+++ b/ui/gfx/transform_util.h
@@ -58,6 +58,9 @@
                               const Transform& transform,
                               const Rect& viewport);
 
+// Computes the Frobenius norm of (a - b).
+GFX_EXPORT float MatrixDistance(const Transform& a, const Transform& b);
+
 }  // namespace gfx
 
 #endif  // UI_GFX_TRANSFORM_UTIL_H_
diff --git a/ui/gl/BUILD.gn b/ui/gl/BUILD.gn
index bbd9363..c596f53 100644
--- a/ui/gl/BUILD.gn
+++ b/ui/gl/BUILD.gn
@@ -81,11 +81,11 @@
     "gl_surface.h",
     "gl_surface_android.cc",
     "gl_surface_mac.cc",
+    "gl_surface_osmesa.cc",
+    "gl_surface_osmesa.h",
     "gl_surface_stub.cc",
     "gl_surface_stub.h",
     "gl_surface_win.cc",
-    "gl_surface_osmesa.cc",
-    "gl_surface_osmesa.h",
     "gl_switches.cc",
     "gl_switches.h",
     "gl_version_info.cc",
@@ -159,6 +159,8 @@
       "gl_context_glx.cc",
       "gl_context_glx.h",
       "gl_context_x11.cc",
+      "gl_egl_api_implementation.cc",
+      "gl_egl_api_implementation.h",
       "gl_glx_api_implementation.cc",
       "gl_glx_api_implementation.h",
       "gl_image_glx.cc",
@@ -167,8 +169,6 @@
       "gl_surface_glx.cc",
       "gl_surface_glx.h",
       "gl_surface_x11.cc",
-      "gl_egl_api_implementation.cc",
-      "gl_egl_api_implementation.h",
     ]
 
     configs += [
@@ -254,9 +254,9 @@
   sources = [
     "gl_bindings_autogen_mock.cc",
     "gl_bindings_autogen_mock.h",
-    "gl_mock_autogen_gl.h",
-    "gl_mock.h",
     "gl_mock.cc",
+    "gl_mock.h",
+    "gl_mock_autogen_gl.h",
   ]
 
   configs += [ "//third_party/khronos:khronos_headers" ]
diff --git a/ui/gl/egl_util.h b/ui/gl/egl_util.h
index c0fe4ce..0ef9095 100644
--- a/ui/gl/egl_util.h
+++ b/ui/gl/egl_util.h
@@ -5,10 +5,12 @@
 #ifndef UI_GL_EGL_UTIL_H_
 #define UI_GL_EGL_UTIL_H_
 
+#include "ui/gl/gl_export.h"
+
 namespace ui {
 
 // Returns the last EGL error as a string.
-const char* GetLastEGLErrorString();
+GL_EXPORT const char* GetLastEGLErrorString();
 
 }  // namespace ui
 
diff --git a/ui/gl/generate_bindings.py b/ui/gl/generate_bindings.py
index 52daf1c..639ec7e 100755
--- a/ui/gl/generate_bindings.py
+++ b/ui/gl/generate_bindings.py
@@ -642,7 +642,7 @@
 { 'return_type': 'void',
   'versions': [{ 'name': 'glGetTransformFeedbackVarying' }],
   'arguments': 'GLuint program, GLuint index, GLsizei bufSize, '
-               'GLsizei* length, GLenum* type, char* name', },
+               'GLsizei* length, GLsizei* size, GLenum* type, char* name', },
 { 'return_type': 'void',
   'names': ['glGetTranslatedShaderSourceANGLE'],
   'arguments':
diff --git a/ui/gl/gl.gyp b/ui/gl/gl.gyp
index 7d97c66..6b173f4 100644
--- a/ui/gl/gl.gyp
+++ b/ui/gl/gl.gyp
@@ -52,9 +52,9 @@
         'gl_context.h',
         'gl_context_android.cc',
         'gl_context_mac.mm',
-        'gl_context_ozone.cc',
         'gl_context_osmesa.cc',
         'gl_context_osmesa.h',
+        'gl_context_ozone.cc',
         'gl_context_stub.cc',
         'gl_context_stub.h',
         'gl_context_stub_with_extensions.cc',
@@ -85,8 +85,8 @@
         'gl_implementation.cc',
         'gl_implementation.h',
         'gl_implementation_android.cc',
-        'gl_implementation_ozone.cc',
         'gl_implementation_mac.cc',
+        'gl_implementation_ozone.cc',
         'gl_implementation_win.cc',
         'gl_implementation_x11.cc',
         'gl_osmesa_api_implementation.cc',
@@ -99,13 +99,13 @@
         'gl_surface.h',
         'gl_surface_android.cc',
         'gl_surface_mac.cc',
+        'gl_surface_osmesa.cc',
+        'gl_surface_osmesa.h',
+        'gl_surface_ozone.cc',
         'gl_surface_stub.cc',
         'gl_surface_stub.h',
         'gl_surface_win.cc',
         'gl_surface_x11.cc',
-        'gl_surface_osmesa.cc',
-        'gl_surface_osmesa.h',
-        'gl_surface_ozone.cc',
         'gl_switches.cc',
         'gl_switches.h',
         'gl_version_info.cc',
@@ -129,14 +129,14 @@
             'gl_bindings_autogen_egl.h',
             'gl_context_egl.cc',
             'gl_context_egl.h',
+            'gl_egl_api_implementation.cc',
+            'gl_egl_api_implementation.h',
             'gl_fence_egl.cc',
             'gl_fence_egl.h',
             'gl_image_egl.cc',
             'gl_image_egl.h',
             'gl_surface_egl.cc',
             'gl_surface_egl.h',
-            'gl_egl_api_implementation.cc',
-            'gl_egl_api_implementation.h',
           ],
           'include_dirs': [
             '<(DEPTH)/third_party/khronos',
@@ -160,14 +160,14 @@
             'gl_bindings_autogen_glx.h',
             'gl_context_glx.cc',
             'gl_context_glx.h',
+            'gl_egl_api_implementation.cc',
+            'gl_egl_api_implementation.h',
             'gl_glx_api_implementation.cc',
             'gl_glx_api_implementation.h',
             'gl_image_glx.cc',
             'gl_image_glx.h',
             'gl_surface_glx.cc',
             'gl_surface_glx.h',
-            'gl_egl_api_implementation.cc',
-            'gl_egl_api_implementation.h',
           ],
           'all_dependent_settings': {
             'defines': [
@@ -280,9 +280,9 @@
       'sources': [
         'gl_bindings_autogen_mock.cc',
         'gl_bindings_autogen_mock.h',
-        'gl_mock_autogen_gl.h',
-        'gl_mock.h',
         'gl_mock.cc',
+        'gl_mock.h',
+        'gl_mock_autogen_gl.h',
       ],
     },
   ],
diff --git a/ui/gl/gl_bindings_api_autogen_gl.h b/ui/gl/gl_bindings_api_autogen_gl.h
index 5aac77c..c886a23 100644
--- a/ui/gl/gl_bindings_api_autogen_gl.h
+++ b/ui/gl/gl_bindings_api_autogen_gl.h
@@ -397,6 +397,7 @@
                                      GLuint index,
                                      GLsizei bufSize,
                                      GLsizei* length,
+                                     GLsizei* size,
                                      GLenum* type,
                                      char* name) override;
 void glGetTranslatedShaderSourceANGLEFn(GLuint shader,
diff --git a/ui/gl/gl_bindings_autogen_gl.cc b/ui/gl/gl_bindings_autogen_gl.cc
index 11383b1..f1100e8 100644
--- a/ui/gl/gl_bindings_autogen_gl.cc
+++ b/ui/gl/gl_bindings_autogen_gl.cc
@@ -3447,15 +3447,17 @@
                                                                 GLuint index,
                                                                 GLsizei bufSize,
                                                                 GLsizei* length,
+                                                                GLsizei* size,
                                                                 GLenum* type,
                                                                 char* name) {
   GL_SERVICE_LOG("glGetTransformFeedbackVarying"
                  << "(" << program << ", " << index << ", " << bufSize << ", "
                  << static_cast<const void*>(length) << ", "
+                 << static_cast<const void*>(size) << ", "
                  << static_cast<const void*>(type) << ", "
                  << static_cast<const void*>(name) << ")");
-  g_driver_gl.debug_fn.glGetTransformFeedbackVaryingFn(program, index, bufSize,
-                                                       length, type, name);
+  g_driver_gl.debug_fn.glGetTransformFeedbackVaryingFn(
+      program, index, bufSize, length, size, type, name);
 }
 
 static void GL_BINDING_CALL
@@ -6776,10 +6778,11 @@
                                                 GLuint index,
                                                 GLsizei bufSize,
                                                 GLsizei* length,
+                                                GLsizei* size,
                                                 GLenum* type,
                                                 char* name) {
   driver_->fn.glGetTransformFeedbackVaryingFn(program, index, bufSize, length,
-                                              type, name);
+                                              size, type, name);
 }
 
 void GLApiBase::glGetTranslatedShaderSourceANGLEFn(GLuint shader,
@@ -8599,12 +8602,13 @@
                                                  GLuint index,
                                                  GLsizei bufSize,
                                                  GLsizei* length,
+                                                 GLsizei* size,
                                                  GLenum* type,
                                                  char* name) {
   TRACE_EVENT_BINARY_EFFICIENT0("gpu",
                                 "TraceGLAPI::glGetTransformFeedbackVarying")
   gl_api_->glGetTransformFeedbackVaryingFn(program, index, bufSize, length,
-                                           type, name);
+                                           size, type, name);
 }
 
 void TraceGLApi::glGetTranslatedShaderSourceANGLEFn(GLuint shader,
@@ -10723,6 +10727,7 @@
                                                      GLuint index,
                                                      GLsizei bufSize,
                                                      GLsizei* length,
+                                                     GLsizei* size,
                                                      GLenum* type,
                                                      char* name) {
   NOTREACHED() << "Trying to call glGetTransformFeedbackVarying() without "
diff --git a/ui/gl/gl_bindings_autogen_gl.h b/ui/gl/gl_bindings_autogen_gl.h
index b38ba3f..b930484 100644
--- a/ui/gl/gl_bindings_autogen_gl.h
+++ b/ui/gl/gl_bindings_autogen_gl.h
@@ -481,6 +481,7 @@
     GLuint index,
     GLsizei bufSize,
     GLsizei* length,
+    GLsizei* size,
     GLenum* type,
     char* name);
 typedef void(GL_BINDING_CALL* glGetTranslatedShaderSourceANGLEProc)(
@@ -1644,6 +1645,7 @@
                                                GLuint index,
                                                GLsizei bufSize,
                                                GLsizei* length,
+                                               GLsizei* size,
                                                GLenum* type,
                                                char* name) = 0;
   virtual void glGetTranslatedShaderSourceANGLEFn(GLuint shader,
diff --git a/ui/gl/gl_bindings_autogen_mock.cc b/ui/gl/gl_bindings_autogen_mock.cc
index 8e84aa3..8d3614b 100644
--- a/ui/gl/gl_bindings_autogen_mock.cc
+++ b/ui/gl/gl_bindings_autogen_mock.cc
@@ -1445,11 +1445,12 @@
                                                     GLuint index,
                                                     GLsizei bufSize,
                                                     GLsizei* length,
+                                                    GLsizei* size,
                                                     GLenum* type,
                                                     char* name) {
   MakeFunctionUnique("glGetTransformFeedbackVarying");
-  interface_->GetTransformFeedbackVarying(program, index, bufSize, length, type,
-                                          name);
+  interface_->GetTransformFeedbackVarying(program, index, bufSize, length, size,
+                                          type, name);
 }
 
 void GL_BINDING_CALL
diff --git a/ui/gl/gl_bindings_autogen_mock.h b/ui/gl/gl_bindings_autogen_mock.h
index 4b1ff4b..52abdae 100644
--- a/ui/gl/gl_bindings_autogen_mock.h
+++ b/ui/gl/gl_bindings_autogen_mock.h
@@ -515,6 +515,7 @@
                                                                GLuint index,
                                                                GLsizei bufSize,
                                                                GLsizei* length,
+                                                               GLsizei* size,
                                                                GLenum* type,
                                                                char* name);
 static void GL_BINDING_CALL
diff --git a/ui/gl/gl_bindings_skia_in_process.cc b/ui/gl/gl_bindings_skia_in_process.cc
index 5ce7821..91d0b0f 100644
--- a/ui/gl/gl_bindings_skia_in_process.cc
+++ b/ui/gl/gl_bindings_skia_in_process.cc
@@ -277,6 +277,10 @@
   glGenVertexArraysOES(n, arrays);
 }
 
+GLvoid StubGLGenerateMipmap(GLenum target){
+  glGenerateMipmapEXT(target);
+}
+
 GLvoid StubGLGetBufferParameteriv(GLenum target, GLenum pname, GLint* params) {
   glGetBufferParameteriv(target, pname, params);
 }
@@ -668,6 +672,7 @@
   functions->fGenQueries = StubGLGenQueries;
   functions->fGenTextures = StubGLGenTextures;
   functions->fGenVertexArrays = StubGLGenVertexArrays;
+  functions->fGenerateMipmap = StubGLGenerateMipmap;
   functions->fGetBufferParameteriv = StubGLGetBufferParameteriv;
   functions->fGetError = StubGLGetError;
   functions->fGetIntegerv = StubGLGetIntegerv;
diff --git a/ui/gl/gl_fence_egl.cc b/ui/gl/gl_fence_egl.cc
index 74f0a01..641b8c2 100644
--- a/ui/gl/gl_fence_egl.cc
+++ b/ui/gl/gl_fence_egl.cc
@@ -45,7 +45,7 @@
   EGLTimeKHR time = EGL_FOREVER_KHR;
   EGLint result = eglClientWaitSyncKHR(display_, sync_, flags, time);
   DCHECK_IMPLIES(!g_ignore_egl_sync_failures,
-                 EGL_TIMEOUT_EXPIRED_KHR == result);
+                 EGL_TIMEOUT_EXPIRED_KHR != result);
   if (result == EGL_FALSE) {
     LOG(ERROR) << "Failed to wait for EGLSync. error:"
                << ui::GetLastEGLErrorString();
diff --git a/ui/gl/gl_mock_autogen_gl.h b/ui/gl/gl_mock_autogen_gl.h
index 470de01..5b47058 100644
--- a/ui/gl/gl_mock_autogen_gl.h
+++ b/ui/gl/gl_mock_autogen_gl.h
@@ -390,11 +390,12 @@
              void(GLenum target, GLenum pname, GLfloat* params));
 MOCK_METHOD3(GetTexParameteriv,
              void(GLenum target, GLenum pname, GLint* params));
-MOCK_METHOD6(GetTransformFeedbackVarying,
+MOCK_METHOD7(GetTransformFeedbackVarying,
              void(GLuint program,
                   GLuint index,
                   GLsizei bufSize,
                   GLsizei* length,
+                  GLsizei* size,
                   GLenum* type,
                   char* name));
 MOCK_METHOD4(
diff --git a/ui/gl/gl_surface_osmesa.cc b/ui/gl/gl_surface_osmesa.cc
index 4c49166..7f1b1ad 100644
--- a/ui/gl/gl_surface_osmesa.cc
+++ b/ui/gl/gl_surface_osmesa.cc
@@ -3,6 +3,7 @@
 // found in the LICENSE file.
 
 #include "base/logging.h"
+#include "base/numerics/safe_math.h"
 #include "third_party/mesa/src/include/GL/osmesa.h"
 #include "ui/gl/gl_bindings.h"
 #include "ui/gl/gl_context.h"
@@ -50,8 +51,17 @@
   // Preserve the old buffer.
   scoped_ptr<int32[]> old_buffer(buffer_.release());
 
+  base::CheckedNumeric<int> checked_size = sizeof(buffer_[0]);
+  checked_size *= new_size.width();
+  checked_size *= new_size.height();
+  if (!checked_size.IsValid())
+    return false;
+
   // Allocate a new one.
   buffer_.reset(new int32[new_size.GetArea()]);
+  if (!buffer_.get())
+    return false;
+
   memset(buffer_.get(), 0, new_size.GetArea() * sizeof(buffer_[0]));
 
   // Copy the old back buffer into the new buffer.
diff --git a/ui/gl/gl_surface_ozone.cc b/ui/gl/gl_surface_ozone.cc
index eecd064..cba706b 100644
--- a/ui/gl/gl_surface_ozone.cc
+++ b/ui/gl/gl_surface_ozone.cc
@@ -105,7 +105,9 @@
                             AcceleratedWidget widget)
       : SurfacelessEGL(gfx::Size()),
         ozone_surface_(ozone_surface.Pass()),
-        widget_(widget) {}
+        widget_(widget),
+        has_implicit_external_sync_(
+            HasEGLExtension("EGL_ARM_implicit_external_sync")) {}
 
   bool Initialize() override {
     if (!SurfacelessEGL::Initialize())
@@ -122,8 +124,8 @@
     return SurfacelessEGL::Resize(size);
   }
   bool SwapBuffers() override {
-    // TODO: this should be replaced by a fence when supported by the driver.
-    glFlush();
+    if (!Flush())
+      return false;
     return ozone_surface_->OnSwapBuffers();
   }
   bool ScheduleOverlayPlane(int z_order,
@@ -143,8 +145,8 @@
     return true;
   }
   bool SwapBuffersAsync(const SwapCompletionCallback& callback) override {
-    // TODO: this should be replaced by a fence when supported by the driver.
-    glFlush();
+    if (!Flush())
+      return false;
     return ozone_surface_->OnSwapBuffersAsync(callback);
   }
   bool PostSubBufferAsync(int x,
@@ -160,11 +162,35 @@
     Destroy();  // EGL surface must be destroyed before SurfaceOzone
   }
 
+  bool Flush() {
+    glFlush();
+    // TODO: the following should be replaced by a per surface flush as it gets
+    // implemented in GL drivers.
+    if (has_implicit_external_sync_) {
+      const EGLint attrib_list[] = {
+          EGL_SYNC_CONDITION_KHR,
+          EGL_SYNC_PRIOR_COMMANDS_IMPLICIT_EXTERNAL_ARM,
+          EGL_NONE};
+      EGLSyncKHR fence =
+          eglCreateSyncKHR(GetDisplay(), EGL_SYNC_FENCE_KHR, attrib_list);
+      if (fence) {
+        // TODO(dbehr): piman@ suggests we could improve here by moving
+        // following wait to right before drmModePageFlip crbug.com/456417.
+        eglClientWaitSyncKHR(GetDisplay(), fence,
+                             EGL_SYNC_FLUSH_COMMANDS_BIT_KHR, EGL_FOREVER_KHR);
+        eglDestroySyncKHR(GetDisplay(), fence);
+      } else {
+        return false;
+      }
+    }
+    return true;
+  }
+
   // The native surface. Deleting this is allowed to free the EGLNativeWindow.
   scoped_ptr<ui::SurfaceOzoneEGL> ozone_surface_;
   AcceleratedWidget widget_;
   scoped_ptr<VSyncProvider> vsync_provider_;
-
+  bool has_implicit_external_sync_;
   DISALLOW_COPY_AND_ASSIGN(GLSurfaceOzoneSurfaceless);
 };