|
| 1 | +// Copyright 2013 The Flutter Authors. All rights reserved. |
| 2 | +// Use of this source code is governed by a BSD-style license that can be |
| 3 | +// found in the LICENSE file. |
| 4 | + |
| 5 | +#include "flutter/fml/logging.h" |
| 6 | + |
| 7 | +#include "flutter/shell/platform/windows/direct_manipulation.h" |
| 8 | +#include "flutter/shell/platform/windows/window_binding_handler_delegate.h" |
| 9 | +#include "flutter/shell/platform/windows/window_win32.h" |
| 10 | + |
| 11 | +#define VERIFY_HR(operation) \ |
| 12 | + if (FAILED(operation)) { \ |
| 13 | + FML_LOG(ERROR) << #operation << " failed"; \ |
| 14 | + manager_ = nullptr; \ |
| 15 | + updateManager_ = nullptr; \ |
| 16 | + viewport_ = nullptr; \ |
| 17 | + return -1; \ |
| 18 | + } |
| 19 | + |
| 20 | +#define WARN_HR(operation) \ |
| 21 | + if (FAILED(operation)) { \ |
| 22 | + FML_LOG(ERROR) << #operation << " failed"; \ |
| 23 | + } |
| 24 | + |
| 25 | +namespace flutter { |
| 26 | + |
| 27 | +STDMETHODIMP DirectManipulationEventHandler::QueryInterface(REFIID iid, |
| 28 | + void** ppv) { |
| 29 | + if ((iid == IID_IUnknown) || |
| 30 | + (iid == IID_IDirectManipulationViewportEventHandler)) { |
| 31 | + *ppv = static_cast<IDirectManipulationViewportEventHandler*>(this); |
| 32 | + AddRef(); |
| 33 | + return S_OK; |
| 34 | + } else if (iid == IID_IDirectManipulationInteractionEventHandler) { |
| 35 | + *ppv = static_cast<IDirectManipulationInteractionEventHandler*>(this); |
| 36 | + AddRef(); |
| 37 | + return S_OK; |
| 38 | + } |
| 39 | + return E_NOINTERFACE; |
| 40 | +} |
| 41 | + |
| 42 | +HRESULT DirectManipulationEventHandler::OnViewportStatusChanged( |
| 43 | + IDirectManipulationViewport* viewport, |
| 44 | + DIRECTMANIPULATION_STATUS current, |
| 45 | + DIRECTMANIPULATION_STATUS previous) { |
| 46 | + if (current == DIRECTMANIPULATION_RUNNING) { |
| 47 | + if (!resetting_) { |
| 48 | + // Not a false event |
| 49 | + if (owner_->binding_handler_delegate) { |
| 50 | + owner_->binding_handler_delegate->OnPointerPanZoomStart( |
| 51 | + (int32_t) reinterpret_cast<int64_t>(this)); |
| 52 | + } |
| 53 | + } |
| 54 | + } else if (previous == DIRECTMANIPULATION_RUNNING) { |
| 55 | + if (resetting_) { |
| 56 | + // The resetting transition has concluded |
| 57 | + resetting_ = false; |
| 58 | + } else { |
| 59 | + if (owner_->binding_handler_delegate) { |
| 60 | + owner_->binding_handler_delegate->OnPointerPanZoomEnd( |
| 61 | + (int32_t) reinterpret_cast<int64_t>(this)); |
| 62 | + } |
| 63 | + // Need to reset the content transform to its original position |
| 64 | + // so that we are ready for the next gesture |
| 65 | + // Use resetting_ flag to prevent sending reset also to the framework |
| 66 | + resetting_ = true; |
| 67 | + RECT rect; |
| 68 | + HRESULT hr = viewport->GetViewportRect(&rect); |
| 69 | + if (FAILED(hr)) { |
| 70 | + FML_LOG(ERROR) << "Failed to get the current viewport rect"; |
| 71 | + return E_FAIL; |
| 72 | + } |
| 73 | + hr = viewport->ZoomToRect(rect.left, rect.top, rect.right, rect.bottom, |
| 74 | + false); |
| 75 | + if (FAILED(hr)) { |
| 76 | + FML_LOG(ERROR) << "Failed to reset the gesture using ZoomToRect"; |
| 77 | + return E_FAIL; |
| 78 | + } |
| 79 | + } |
| 80 | + } |
| 81 | + return S_OK; |
| 82 | +} |
| 83 | + |
| 84 | +HRESULT DirectManipulationEventHandler::OnViewportUpdated( |
| 85 | + IDirectManipulationViewport* viewport) { |
| 86 | + return S_OK; |
| 87 | +} |
| 88 | + |
| 89 | +HRESULT DirectManipulationEventHandler::OnContentUpdated( |
| 90 | + IDirectManipulationViewport* viewport, |
| 91 | + IDirectManipulationContent* content) { |
| 92 | + float transform[6]; |
| 93 | + HRESULT hr = content->GetContentTransform(transform, ARRAYSIZE(transform)); |
| 94 | + if (FAILED(hr)) { |
| 95 | + FML_LOG(ERROR) << "GetContentTransform failed"; |
| 96 | + return S_OK; |
| 97 | + } |
| 98 | + if (!resetting_) { |
| 99 | + // DirectManipulation provides updates with very high precision. If the user |
| 100 | + // holds their fingers steady on a trackpad, DirectManipulation sends |
| 101 | + // jittery updates. This calculation will reduce the precision of the scale |
| 102 | + // value of the event to avoid jitter |
| 103 | + const int mantissa_bits_chop = 2; |
| 104 | + const float factor = (1 << mantissa_bits_chop) + 1; |
| 105 | + float c = factor * transform[0]; |
| 106 | + float scale = c - (c - transform[0]); |
| 107 | + float pan_x = transform[4]; |
| 108 | + float pan_y = transform[5]; |
| 109 | + if (owner_->binding_handler_delegate) { |
| 110 | + owner_->binding_handler_delegate->OnPointerPanZoomUpdate( |
| 111 | + (int32_t) reinterpret_cast<int64_t>(this), pan_x, pan_y, scale, 0); |
| 112 | + } |
| 113 | + } |
| 114 | + return S_OK; |
| 115 | +} |
| 116 | + |
| 117 | +HRESULT DirectManipulationEventHandler::OnInteraction( |
| 118 | + IDirectManipulationViewport2* viewport, |
| 119 | + DIRECTMANIPULATION_INTERACTION_TYPE interaction) { |
| 120 | + return S_OK; |
| 121 | +} |
| 122 | + |
| 123 | +ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::AddRef() { |
| 124 | + RefCountedThreadSafe::AddRef(); |
| 125 | + return 0; |
| 126 | +} |
| 127 | + |
| 128 | +ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::Release() { |
| 129 | + RefCountedThreadSafe::Release(); |
| 130 | + return 0; |
| 131 | +} |
| 132 | + |
| 133 | +DirectManipulationOwner::DirectManipulationOwner(WindowWin32* window) |
| 134 | + : window_(window) {} |
| 135 | + |
| 136 | +int DirectManipulationOwner::Init(unsigned int width, unsigned int height) { |
| 137 | + VERIFY_HR(CoCreateInstance(CLSID_DirectManipulationManager, nullptr, |
| 138 | + CLSCTX_INPROC_SERVER, |
| 139 | + IID_IDirectManipulationManager, &manager_)); |
| 140 | + VERIFY_HR(manager_->GetUpdateManager(IID_IDirectManipulationUpdateManager, |
| 141 | + &updateManager_)); |
| 142 | + VERIFY_HR(manager_->CreateViewport(nullptr, window_->GetWindowHandle(), |
| 143 | + IID_IDirectManipulationViewport, |
| 144 | + &viewport_)); |
| 145 | + DIRECTMANIPULATION_CONFIGURATION configuration = |
| 146 | + DIRECTMANIPULATION_CONFIGURATION_INTERACTION | |
| 147 | + DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X | |
| 148 | + DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y | |
| 149 | + DIRECTMANIPULATION_CONFIGURATION_SCALING; |
| 150 | + VERIFY_HR(viewport_->ActivateConfiguration(configuration)); |
| 151 | + VERIFY_HR(viewport_->SetViewportOptions( |
| 152 | + DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE)); |
| 153 | + handler_ = fml::MakeRefCounted<DirectManipulationEventHandler>(window_, this); |
| 154 | + VERIFY_HR(viewport_->AddEventHandler( |
| 155 | + window_->GetWindowHandle(), handler_.get(), &viewportHandlerCookie_)); |
| 156 | + RECT rect = {0, 0, (LONG)width, (LONG)height}; |
| 157 | + VERIFY_HR(viewport_->SetViewportRect(&rect)); |
| 158 | + VERIFY_HR(manager_->Activate(window_->GetWindowHandle())); |
| 159 | + VERIFY_HR(viewport_->Enable()); |
| 160 | + VERIFY_HR(updateManager_->Update(nullptr)); |
| 161 | + return 0; |
| 162 | +} |
| 163 | + |
| 164 | +void DirectManipulationOwner::ResizeViewport(unsigned int width, |
| 165 | + unsigned int height) { |
| 166 | + if (viewport_) { |
| 167 | + RECT rect = {0, 0, (LONG)width, (LONG)height}; |
| 168 | + WARN_HR(viewport_->SetViewportRect(&rect)); |
| 169 | + } |
| 170 | +} |
| 171 | + |
| 172 | +void DirectManipulationOwner::Destroy() { |
| 173 | + if (handler_) { |
| 174 | + handler_->window_ = nullptr; |
| 175 | + handler_->owner_ = nullptr; |
| 176 | + } |
| 177 | + |
| 178 | + if (viewport_) { |
| 179 | + WARN_HR(viewport_->Disable()); |
| 180 | + WARN_HR(viewport_->Disable()); |
| 181 | + WARN_HR(viewport_->RemoveEventHandler(viewportHandlerCookie_)); |
| 182 | + WARN_HR(viewport_->Abandon()); |
| 183 | + } |
| 184 | + |
| 185 | + if (window_ && manager_) { |
| 186 | + WARN_HR(manager_->Deactivate(window_->GetWindowHandle())); |
| 187 | + } |
| 188 | + |
| 189 | + handler_ = nullptr; |
| 190 | + viewport_ = nullptr; |
| 191 | + updateManager_ = nullptr; |
| 192 | + manager_ = nullptr; |
| 193 | + window_ = nullptr; |
| 194 | +} |
| 195 | + |
| 196 | +void DirectManipulationOwner::SetContact(UINT contactId) { |
| 197 | + if (viewport_) { |
| 198 | + viewport_->SetContact(contactId); |
| 199 | + } |
| 200 | +} |
| 201 | + |
| 202 | +void DirectManipulationOwner::SetBindingHandlerDelegate( |
| 203 | + WindowBindingHandlerDelegate* delegate) { |
| 204 | + binding_handler_delegate = delegate; |
| 205 | +} |
| 206 | + |
| 207 | +void DirectManipulationOwner::Update() { |
| 208 | + if (updateManager_) { |
| 209 | + HRESULT hr = updateManager_->Update(nullptr); |
| 210 | + if (FAILED(hr)) { |
| 211 | + FML_LOG(ERROR) << "updateManager_->Update failed"; |
| 212 | + auto error = GetLastError(); |
| 213 | + FML_LOG(ERROR) << error; |
| 214 | + LPWSTR message = nullptr; |
| 215 | + size_t size = FormatMessageW( |
| 216 | + FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM | |
| 217 | + FORMAT_MESSAGE_IGNORE_INSERTS, |
| 218 | + NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT), |
| 219 | + reinterpret_cast<LPWSTR>(&message), 0, NULL); |
| 220 | + FML_LOG(ERROR) << message; |
| 221 | + } |
| 222 | + } |
| 223 | +} |
| 224 | + |
| 225 | +} // namespace flutter |
0 commit comments