Improve scroll performance on Windows 10 with high precision touchpads.

Chrome scrolling performance on Windows 10 with high precision touch pads, is very janky and jumpy.
Windows 10 introduces a new API called Direct Manipulation which allows consumers to support smooth
scrolling, panning etc. Mouse wheel messages are dispatched differently based on whether the window
is a Direct manipulation consumer. Microsoft recommends supporting Direct Manipulation on Windows 10
and above.

This is an intial patch to do the bare minimum to register Chrome as a Direct Manipulation consumer.
The associated functionality is provided by the newly added DirectManipulationHelper class which lives
as a member of the LegacyRenderWidgetHostHWND class which ensures that these changes are isolated cleanly.

This does smoothen scrolling on these devices. However there is still work to be done

BUG=517183,517187

Review URL: https://codereview.chromium.org/1283913002

Cr-Commit-Position: refs/heads/master@{#342962}
diff --git a/content/browser/renderer_host/legacy_render_widget_host_win.cc b/content/browser/renderer_host/legacy_render_widget_host_win.cc
index 5126ad2..8dcdaf6 100644
--- a/content/browser/renderer_host/legacy_render_widget_host_win.cc
+++ b/content/browser/renderer_host/legacy_render_widget_host_win.cc
@@ -18,6 +18,7 @@
 #include "ui/base/win/internal_constants.h"
 #include "ui/base/win/window_event_target.h"
 #include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/win/direct_manipulation.h"
 #include "ui/gfx/win/dpi.h"
 
 namespace content {
@@ -75,6 +76,8 @@
 
 void LegacyRenderWidgetHostHWND::Show() {
   ::ShowWindow(hwnd(), SW_SHOW);
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->Activate(hwnd());
 }
 
 void LegacyRenderWidgetHostHWND::Hide() {
@@ -86,6 +89,8 @@
   ::SetWindowPos(hwnd(), NULL, bounds_in_pixel.x(), bounds_in_pixel.y(),
                  bounds_in_pixel.width(), bounds_in_pixel.height(),
                  SWP_NOREDRAW);
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->SetBounds(bounds_in_pixel);
 }
 
 void LegacyRenderWidgetHostHWND::OnFinalMessage(HWND hwnd) {
@@ -126,6 +131,13 @@
                    CHILDID_SELF);
   }
 
+  // Direct Manipulation is enabled on Windows 10+. The CreateInstance function
+  // returns NULL if Direct Manipulation is not available.
+  direct_manipulation_helper_ =
+      gfx::win::DirectManipulationHelper::CreateInstance();
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->Initialize(hwnd());
+
   return !!SUCCEEDED(hr);
 }
 
@@ -242,6 +254,12 @@
       handled = TRUE;
     }
   }
+
+  if (direct_manipulation_helper_ &&
+      (message == WM_MOUSEWHEEL || message == WM_MOUSEHWHEEL)) {
+    direct_manipulation_helper_->HandleMouseWheel(hwnd(), message, w_param,
+        l_param);
+  }
   return ret;
 }
 
diff --git a/content/browser/renderer_host/legacy_render_widget_host_win.h b/content/browser/renderer_host/legacy_render_widget_host_win.h
index 70f6859..0980508 100644
--- a/content/browser/renderer_host/legacy_render_widget_host_win.h
+++ b/content/browser/renderer_host/legacy_render_widget_host_win.h
@@ -11,10 +11,17 @@
 #include <oleacc.h>
 
 #include "base/basictypes.h"
+#include "base/memory/scoped_ptr.h"
 #include "base/win/scoped_comptr.h"
 #include "content/common/content_export.h"
 #include "ui/gfx/geometry/rect.h"
 
+namespace gfx {
+namespace win {
+class DirectManipulationHelper;
+}  // namespace win
+}  // namespace gfx
+
 namespace ui {
 class WindowEventTarget;
 }
@@ -143,6 +150,11 @@
 
   RenderWidgetHostViewAura* host_;
 
+  // This class provides functionality to register the legacy window as a
+  // Direct Manipulation consumer. This allows us to support smooth scroll
+  // in Chrome on Windows 10.
+  scoped_ptr<gfx::win::DirectManipulationHelper> direct_manipulation_helper_;
+
   DISALLOW_COPY_AND_ASSIGN(LegacyRenderWidgetHostHWND);
 };
 
diff --git a/ui/gfx/BUILD.gn b/ui/gfx/BUILD.gn
index 072072b..58b493f 100644
--- a/ui/gfx/BUILD.gn
+++ b/ui/gfx/BUILD.gn
@@ -215,6 +215,8 @@
     "utf16_indexing.h",
     "vector_icons_public.h",
     "vsync_provider.h",
+    "win/direct_manipulation.cc",
+    "win/direct_manipulation.h",
     "win/direct_write.cc",
     "win/direct_write.h",
     "win/dpi.cc",
diff --git a/ui/gfx/gfx.gyp b/ui/gfx/gfx.gyp
index 113e2fb..a92eca1 100644
--- a/ui/gfx/gfx.gyp
+++ b/ui/gfx/gfx.gyp
@@ -296,6 +296,8 @@
         'utf16_indexing.cc',
         'utf16_indexing.h',
         'vsync_provider.h',
+        'win/direct_manipulation.cc',
+        'win/direct_manipulation.h',
         'win/direct_write.cc',
         'win/direct_write.h',
         'win/dpi.cc',
diff --git a/ui/gfx/win/direct_manipulation.cc b/ui/gfx/win/direct_manipulation.cc
new file mode 100644
index 0000000..919bcca
--- /dev/null
+++ b/ui/gfx/win/direct_manipulation.cc
@@ -0,0 +1,109 @@
+// Copyright 2015 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 "ui/gfx/win/direct_manipulation.h"
+
+#include "base/basictypes.h"
+#include "base/win/windows_version.h"
+
+namespace gfx {
+namespace win {
+
+// static
+scoped_ptr<DirectManipulationHelper>
+DirectManipulationHelper::CreateInstance() {
+  scoped_ptr<DirectManipulationHelper> instance;
+
+  if (base::win::GetVersion() >= base::win::VERSION_WIN10)
+    instance.reset(new DirectManipulationHelper);
+
+  return instance.Pass();
+}
+
+DirectManipulationHelper::DirectManipulationHelper() {}
+
+void DirectManipulationHelper::Initialize(HWND window) {
+  DCHECK(::IsWindow(window));
+
+  // TODO(ananta)
+  // Remove the CHECK statements here and below and replace them with logs
+  // when this code stabilizes.
+  HRESULT hr = manager_.CreateInstance(CLSID_DirectManipulationManager,
+    nullptr, CLSCTX_INPROC_SERVER);
+  CHECK(SUCCEEDED(hr));
+
+  hr = compositor_.CreateInstance(CLSID_DCompManipulationCompositor,
+      nullptr, CLSCTX_INPROC_SERVER);
+  CHECK(SUCCEEDED(hr));
+
+  hr = manager_->GetUpdateManager(IID_PPV_ARGS(update_manager_.Receive()));
+  CHECK(SUCCEEDED(hr));
+
+  hr = compositor_->SetUpdateManager(update_manager_.get());
+  CHECK(SUCCEEDED(hr));
+
+  hr = frame_info_.QueryFrom(compositor_.get());
+  CHECK(SUCCEEDED(hr));
+
+  hr = manager_->CreateViewport(frame_info_.get(), window,
+      IID_PPV_ARGS(view_port_outer_.Receive()));
+  CHECK(SUCCEEDED(hr));
+
+  //
+  // Enable the desired configuration for each viewport.
+  //
+  DIRECTMANIPULATION_CONFIGURATION configuration =
+      DIRECTMANIPULATION_CONFIGURATION_INTERACTION
+      | DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X
+      | DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y
+      | DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_INERTIA
+      | DIRECTMANIPULATION_CONFIGURATION_RAILS_X
+      | DIRECTMANIPULATION_CONFIGURATION_RAILS_Y
+      | DIRECTMANIPULATION_CONFIGURATION_SCALING
+      | DIRECTMANIPULATION_CONFIGURATION_SCALING_INERTIA;
+
+  hr = view_port_outer_->ActivateConfiguration(configuration);
+  CHECK(SUCCEEDED(hr));
+}
+
+void DirectManipulationHelper::SetBounds(const gfx::Rect& bounds) {
+  base::win::ScopedComPtr<IDirectManipulationPrimaryContent>
+      primary_content_outer;
+  HRESULT hr = view_port_outer_->GetPrimaryContent(
+      IID_PPV_ARGS(primary_content_outer.Receive()));
+  CHECK(SUCCEEDED(hr));
+
+  base::win::ScopedComPtr<IDirectManipulationContent> content_outer;
+  hr = content_outer.QueryFrom(primary_content_outer.get());
+  CHECK(SUCCEEDED(hr));
+
+  RECT rect = bounds.ToRECT();
+
+  hr = view_port_outer_->SetViewportRect(&rect);
+  CHECK(SUCCEEDED(hr));
+
+  hr = content_outer->SetContentRect(&rect);
+  CHECK(SUCCEEDED(hr));
+}
+
+void DirectManipulationHelper::Activate(HWND window) {
+  DCHECK(::IsWindow(window));
+  HRESULT hr = manager_->Activate(window);
+  CHECK(SUCCEEDED(hr));
+}
+
+void DirectManipulationHelper:: HandleMouseWheel(HWND window, UINT message,
+    WPARAM w_param, LPARAM l_param) {
+  MSG msg = { window, message, w_param, l_param};
+
+  HRESULT hr = view_port_outer_->SetContact(DIRECTMANIPULATION_MOUSEFOCUS);
+  if (SUCCEEDED(hr)) {
+    BOOL handled = FALSE;
+    manager_->ProcessInput(&msg, &handled);
+    view_port_outer_->ReleaseContact(DIRECTMANIPULATION_MOUSEFOCUS);
+  }
+}
+
+}  // namespace win.
+}  // namespace gfx.
\ No newline at end of file
diff --git a/ui/gfx/win/direct_manipulation.h b/ui/gfx/win/direct_manipulation.h
new file mode 100644
index 0000000..518e4132
--- /dev/null
+++ b/ui/gfx/win/direct_manipulation.h
@@ -0,0 +1,78 @@
+// Copyright 2015 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.
+
+#ifndef UI_GFX_WIN_DIRECT_MANIPULATION_H_
+#define UI_GFX_WIN_DIRECT_MANIPULATION_H_
+
+#include <directmanipulation.h>
+
+#include "base/memory/scoped_ptr.h"
+#include "base/win/scoped_comptr.h"
+#include "ui/gfx/geometry/rect.h"
+#include "ui/gfx/gfx_export.h"
+
+namespace gfx {
+namespace win {
+
+// Windows 10 provides a new API called Direct Manipulation which generates
+// smooth scroll events via WM_MOUSEWHEEL messages with predictable deltas
+// on high precision touch pads. This basically requires the application window
+// to register as a Direct Manipulation consumer. The way mouse wheel messages
+// are dispatched is
+// 1. The foreground window is checked to see if it is a Direct Manipulation
+//    consumer.
+// 2. If it is then Direct Manipulation takes over and sends the following
+//    messages. WM_POINTERACTIVATE, WM_POINTERDOWN and DM_POINTERHITTEST.
+// 3. It then posts WM_MOUSEWHEEL messages with precision deltas which vary
+//    based on the amount of the scroll.
+// 4. If the foreground window is not a Direct Manipulation consumer, it
+//    then takes a fallback route where it posts WM_MOUSEWHEEL messages
+//    with precision but varying deltas to the window. There is a also
+//    a slight delay in receiving the first set of mouse wheel messages.
+//    This causes scrolling to appear janky and jumpy.
+// Our approach for addressing this is to do the absolute minimum to
+// register our window as a Direct Manipulation consumer. This class
+// provides the necessary functionality to register the passed in HWND as a
+// Direct Manipulation consumer. We don't rely on Direct manipulation
+// to do the smooth scrolling in the background thread as documented on
+// msdn.
+class GFX_EXPORT DirectManipulationHelper {
+ public:
+  // Creates an instance of this class if Direct Manipulation is enabled on
+  // the platform. If not returns NULL.
+  static scoped_ptr<DirectManipulationHelper> CreateInstance();
+
+  // This function instantiates Direct Manipulation and creates a viewport for
+  // the passed in |window|.
+  // consumer. Most of the code is boiler plate and is based on the sample.
+  void Initialize(HWND window);
+
+  // Sets the bounds of the fake Direct manipulation viewport to match those
+  // of the legacy window.
+  void SetBounds(const gfx::Rect& bounds);
+
+  // Registers the passed in |window| as a Direct Manipulation consumer.
+  void Activate(HWND window);
+
+  // Passes the WM_MOUSEWHEEL messages to Direct Manipulation. This is for
+  // logistics purposes.
+  void HandleMouseWheel(HWND window, UINT message, WPARAM w_param,
+      LPARAM l_param);
+
+ private:
+  DirectManipulationHelper();
+
+  base::win::ScopedComPtr<IDirectManipulationManager2> manager_;
+  base::win::ScopedComPtr<IDirectManipulationCompositor> compositor_;
+  base::win::ScopedComPtr<IDirectManipulationUpdateManager> update_manager_;
+  base::win::ScopedComPtr<IDirectManipulationFrameInfoProvider> frame_info_;
+  base::win::ScopedComPtr<IDirectManipulationViewport2> view_port_outer_;
+
+  DISALLOW_COPY_AND_ASSIGN(DirectManipulationHelper);
+};
+
+}  // namespace win
+}  // namespace gfx
+
+#endif  // UI_GFX_WIN_DIRECT_MANIPULATION_H_
diff --git a/ui/views/win/hwnd_message_handler.cc b/ui/views/win/hwnd_message_handler.cc
index 5cadce1..ab72680 100644
--- a/ui/views/win/hwnd_message_handler.cc
+++ b/ui/views/win/hwnd_message_handler.cc
@@ -32,6 +32,7 @@
 #include "ui/gfx/path.h"
 #include "ui/gfx/path_win.h"
 #include "ui/gfx/screen.h"
+#include "ui/gfx/win/direct_manipulation.h"
 #include "ui/gfx/win/dpi.h"
 #include "ui/gfx/win/hwnd_util.h"
 #include "ui/native_theme/native_theme_win.h"
@@ -372,6 +373,13 @@
   prop_window_target_.reset(new ui::ViewProp(hwnd(),
                             ui::WindowEventTarget::kWin32InputEventTarget,
                             static_cast<ui::WindowEventTarget*>(this)));
+
+  // Direct Manipulation is enabled on Windows 10+. The CreateInstance function
+  // returns NULL if Direct Manipulation is not available.
+  direct_manipulation_helper_ =
+      gfx::win::DirectManipulationHelper::CreateInstance();
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->Initialize(hwnd());
 }
 
 void HWNDMessageHandler::InitModalType(ui::ModalType modal_type) {
@@ -513,6 +521,9 @@
     delegate_->HandleClientSizeChanged(GetClientAreaBounds().size());
     ResetWindowRegion(false, true);
   }
+
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->SetBounds(bounds_in_pixels);
 }
 
 void HWNDMessageHandler::SetSize(const gfx::Size& size) {
@@ -554,6 +565,8 @@
       ShowWindowWithState(ui::SHOW_STATE_INACTIVE);
     }
   }
+  if (direct_manipulation_helper_)
+    direct_manipulation_helper_->Activate(hwnd());
 }
 
 void HWNDMessageHandler::ShowWindowWithState(ui::WindowShowState show_state) {
@@ -2671,6 +2684,13 @@
 
   if (!ref.get())
     return 0;
+
+  if (direct_manipulation_helper_ && track_mouse &&
+      (message == WM_MOUSEWHEEL || message == WM_MOUSEHWHEEL)) {
+    direct_manipulation_helper_->HandleMouseWheel(hwnd(), message, w_param,
+        l_param);
+  }
+
   if (!handled && message == WM_NCLBUTTONDOWN && w_param != HTSYSMENU &&
       delegate_->IsUsingCustomFrame()) {
     // TODO(vadimt): Remove ScopedTracker below once crbug.com/440919 is fixed.
diff --git a/ui/views/win/hwnd_message_handler.h b/ui/views/win/hwnd_message_handler.h
index 79374fd..e7fc8af 100644
--- a/ui/views/win/hwnd_message_handler.h
+++ b/ui/views/win/hwnd_message_handler.h
@@ -30,7 +30,10 @@
 class Canvas;
 class ImageSkia;
 class Insets;
-}
+namespace win {
+class DirectManipulationHelper;
+}  // namespace win
+}  // namespace gfx
 
 namespace ui  {
 class ViewProp;
@@ -593,6 +596,11 @@
   // The factory used with BEGIN_SAFE_MSG_MAP_EX.
   base::WeakPtrFactory<HWNDMessageHandler> weak_factory_;
 
+  // This class provides functionality to register the legacy window as a
+  // Direct Manipulation consumer. This allows us to support smooth scroll
+  // in Chrome on Windows 10.
+  scoped_ptr<gfx::win::DirectManipulationHelper> direct_manipulation_helper_;
+
   DISALLOW_COPY_AND_ASSIGN(HWNDMessageHandler);
 };