File: direct_manipulation_helper_win.cc

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (265 lines) | stat: -rw-r--r-- 7,754 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "content/browser/renderer_host/direct_manipulation_helper_win.h"

#include <objbase.h>

#include <cmath>
#include <utility>

#include "base/logging.h"
#include "base/memory/ptr_util.h"
#include "base/win/win_util.h"
#include "third_party/abseil-cpp/absl/cleanup/cleanup.h"
#include "ui/base/ui_base_features.h"
#include "ui/base/win/window_event_target.h"
#include "ui/compositor/compositor.h"
#include "ui/compositor/compositor_animation_observer.h"
#include "ui/display/win/screen_win.h"
#include "ui/gfx/geometry/rect.h"

namespace content {

namespace {

constexpr gfx::Size kDefaultSize{1000, 1000};

}  // namespace

// static
std::unique_ptr<DirectManipulationHelper>
DirectManipulationHelper::CreateInstance(HWND window) {
  if (!::IsWindow(window)) {
    return nullptr;
  }

  // IDirectManipulationUpdateManager is the first COM object created by the
  // application to retrieve other objects in the Direct Manipulation API.
  // It also serves to activate and deactivate Direct Manipulation functionality
  // on a per-HWND basis.
  ComPtr<IDirectManipulationManager> manager;
  HRESULT hr = ::CoCreateInstance(CLSID_DirectManipulationManager, nullptr,
                                  CLSCTX_INPROC_SERVER, IID_PPV_ARGS(&manager));
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  return CreateInstanceImpl(std::move(manager), window);
}

// static
std::unique_ptr<DirectManipulationHelper>
DirectManipulationHelper::CreateInstanceForTesting(
    ComPtr<IDirectManipulationManager> manager) {
  return CreateInstanceImpl(std::move(manager), /*window=*/nullptr);
}

// static
std::unique_ptr<DirectManipulationHelper>
DirectManipulationHelper::CreateInstanceImpl(
    ComPtr<IDirectManipulationManager> manager,
    HWND window) {
  // Since we want to use fake viewport, we need UpdateManager to tell a fake
  // fake render frame.
  ComPtr<IDirectManipulationUpdateManager> update_manager;
  HRESULT hr = manager->GetUpdateManager(IID_PPV_ARGS(&update_manager));
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  ComPtr<IDirectManipulationViewport> viewport;
  hr = manager->CreateViewport(nullptr, window, IID_PPV_ARGS(&viewport));
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  absl::Cleanup clean_up_viewport_on_error = [&viewport] {
    viewport->Abandon();
  };

  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;

  hr = viewport->ActivateConfiguration(configuration);
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  // Since we are using fake viewport and only want to use Direct Manipulation
  // for touchpad, we need to use MANUALUPDATE option.
  hr = viewport->SetViewportOptions(
      DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE);
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  // Set default rect for viewport before activate.
  RECT rect = gfx::Rect(kDefaultSize).ToRECT();
  hr = viewport->SetViewportRect(&rect);
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  hr = manager->Activate(window);
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  absl::Cleanup deactivate_manager_on_error = [&manager, &window] {
    manager->Deactivate(window);
  };

  hr = viewport->Enable();
  if (!SUCCEEDED(hr)) {
    return nullptr;
  }

  std::move(deactivate_manager_on_error).Cancel();
  std::move(clean_up_viewport_on_error).Cancel();

  return base::WrapUnique(new DirectManipulationHelper(
      std::move(manager), std::move(update_manager), std::move(viewport),
      window));
}

DirectManipulationHelper::DirectManipulationHelper(
    ComPtr<IDirectManipulationManager> manager,
    ComPtr<IDirectManipulationUpdateManager> update_manager,
    ComPtr<IDirectManipulationViewport> viewport,
    HWND window)
    : manager_(std::move(manager)),
      update_manager_(std::move(update_manager)),
      viewport_(std::move(viewport)),
      window_(window),
      size_in_pixels_(kDefaultSize) {}

DirectManipulationHelper::~DirectManipulationHelper() {
  Destroy();
}

void DirectManipulationHelper::OnAnimationStep(base::TimeTicks timestamp) {
  // Simulate 1 frame in update_manager_.
  update_manager_->Update(nullptr);
}

void DirectManipulationHelper::OnCompositingShuttingDown(
    ui::Compositor* notifying_compositor) {
  DCHECK_EQ(notifying_compositor, compositor());
  Destroy();
}

void DirectManipulationHelper::UpdateEventHandler(
    base::WeakPtr<aura::WindowTreeHost> window_tree_host,
    ui::WindowEventTarget* event_target) {
  if (window_tree_host.get() != window_tree_host_.get() &&
      has_animation_observer_) {
    RemoveAnimationObserver();
  }

  if (event_handler_) {
    event_handler_.Reset();
    viewport_->Stop();
    viewport_->RemoveEventHandler(view_port_handler_cookie_);
  }

  window_tree_host_ = window_tree_host;
  event_target_ = event_target;

  if (!event_target) {
    // No need for an event handler without a target.
    return;
  }

  event_handler_ = Microsoft::WRL::Make<DirectManipulationEventHandler>(
      weak_factory_.GetWeakPtr());
  event_handler_->SetViewportSizeInPixels(size_in_pixels_);

  // We got Direct Manipulation transform from
  // IDirectManipulationViewportEventHandler.
  HRESULT hr = viewport_->AddEventHandler(window_, event_handler_.Get(),
                                          &view_port_handler_cookie_);
  if (!SUCCEEDED(hr)) {
    event_handler_.Reset();
    return;
  }

  update_manager_->Update(nullptr);
}

void DirectManipulationHelper::SetSizeInPixels(
    const gfx::Size& size_in_pixels) {
  if (size_in_pixels == size_in_pixels_) {
    return;
  }

  size_in_pixels_ = size_in_pixels;

  if (event_handler_) {
    event_handler_->SetViewportSizeInPixels(size_in_pixels);
  }

  HRESULT hr = viewport_->Stop();
  if (!SUCCEEDED(hr))
    return;

  RECT rect = gfx::Rect(size_in_pixels).ToRECT();
  hr = viewport_->SetViewportRect(&rect);
}

void DirectManipulationHelper::OnPointerHitTest(WPARAM w_param) {
  if (!event_handler_) {
    return;
  }

  // Update the device scale factor.
  event_handler_->SetDeviceScaleFactor(
      display::win::GetScreenWin()->GetScaleFactorForHWND(window_));

  // Only DM_POINTERHITTEST can be the first message of input sequence of
  // touchpad input.
  // TODO(chaopeng) Check if Windows API changes:
  // For WM_POINTER, the pointer type will show the event from mouse.
  // For WM_POINTERACTIVATE, the pointer id will be different with the following
  // message.
  UINT32 pointer_id = GET_POINTERID_WPARAM(w_param);
  POINTER_INPUT_TYPE pointer_type;
  if (::GetPointerType(pointer_id, &pointer_type) &&
      pointer_type == PT_TOUCHPAD) {
    viewport_->SetContact(pointer_id);
  }
}

void DirectManipulationHelper::AddAnimationObserver() {
  if (compositor()) {
    compositor()->AddAnimationObserver(this);
  }
  has_animation_observer_ = true;
}

void DirectManipulationHelper::RemoveAnimationObserver() {
  if (compositor()) {
    compositor()->RemoveAnimationObserver(this);
  }
  has_animation_observer_ = false;
}

void DirectManipulationHelper::SetDeviceScaleFactorForTesting(float factor) {
  DCHECK(event_handler_);
  event_handler_->SetDeviceScaleFactor(factor);
}

void DirectManipulationHelper::Destroy() {
  UpdateEventHandler(nullptr, nullptr);
  viewport_->Abandon();
  manager_->Deactivate(window_);
}

}  // namespace content