Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 3 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -2286,6 +2286,9 @@ FILE: ../../../flutter/shell/platform/windows/client_wrapper/include/flutter/plu
FILE: ../../../flutter/shell/platform/windows/client_wrapper/plugin_registrar_windows_unittests.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.cc
FILE: ../../../flutter/shell/platform/windows/cursor_handler.h
FILE: ../../../flutter/shell/platform/windows/direct_manipulation.cc
FILE: ../../../flutter/shell/platform/windows/direct_manipulation.h
FILE: ../../../flutter/shell/platform/windows/direct_manipulation_unittests.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.cc
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32.h
FILE: ../../../flutter/shell/platform/windows/dpi_utils_win32_unittests.cc
Expand Down
4 changes: 4 additions & 0 deletions shell/platform/windows/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,8 @@ source_set("flutter_windows_source") {
"angle_surface_manager.h",
"cursor_handler.cc",
"cursor_handler.h",
"direct_manipulation.cc",
"direct_manipulation.h",
"dpi_utils_win32.cc",
"dpi_utils_win32.h",
"event_watcher_win32.cc",
Expand Down Expand Up @@ -130,6 +132,7 @@ source_set("flutter_windows_source") {

deps = [
":flutter_windows_headers",
"//flutter/fml:fml",
"//flutter/shell/platform/common:common_cpp",
"//flutter/shell/platform/common:common_cpp_input",
"//flutter/shell/platform/common:common_cpp_switches",
Expand Down Expand Up @@ -170,6 +173,7 @@ executable("flutter_windows_unittests") {
# Common Windows test sources.
sources = [
"accessibility_bridge_delegate_win32_unittests.cc",
"direct_manipulation_unittests.cc",
"dpi_utils_win32_unittests.cc",
"flutter_project_bundle_unittests.cc",
"flutter_window_win32_unittests.cc",
Expand Down
222 changes: 222 additions & 0 deletions shell/platform/windows/direct_manipulation.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,222 @@
// Copyright 2013 The Flutter 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 "flutter/fml/logging.h"

#include "flutter/shell/platform/windows/direct_manipulation.h"
#include "flutter/shell/platform/windows/window_binding_handler_delegate.h"
#include "flutter/shell/platform/windows/window_win32.h"

#define RETURN_IF_FAILED(operation) \
if (FAILED(operation)) { \
FML_LOG(ERROR) << #operation << " failed"; \
manager_ = nullptr; \
updateManager_ = nullptr; \
viewport_ = nullptr; \
return -1; \
}

#define WARN_IF_FAILED(operation) \
if (FAILED(operation)) { \
FML_LOG(ERROR) << #operation << " failed"; \
}

namespace flutter {

STDMETHODIMP DirectManipulationEventHandler::QueryInterface(REFIID iid,
void** ppv) {
if ((iid == IID_IUnknown) ||
(iid == IID_IDirectManipulationViewportEventHandler)) {
*ppv = static_cast<IDirectManipulationViewportEventHandler*>(this);
AddRef();
return S_OK;
} else if (iid == IID_IDirectManipulationInteractionEventHandler) {
*ppv = static_cast<IDirectManipulationInteractionEventHandler*>(this);
AddRef();
return S_OK;
}
return E_NOINTERFACE;
}

HRESULT DirectManipulationEventHandler::OnViewportStatusChanged(
IDirectManipulationViewport* viewport,
DIRECTMANIPULATION_STATUS current,
DIRECTMANIPULATION_STATUS previous) {
if (during_synthesized_reset_ && previous == DIRECTMANIPULATION_RUNNING) {
during_synthesized_reset_ = false;
} else if (current == DIRECTMANIPULATION_RUNNING) {
if (!during_synthesized_reset_) {
// Not a false event.
if (owner_->binding_handler_delegate) {
owner_->binding_handler_delegate->OnPointerPanZoomStart(
(int32_t) reinterpret_cast<int64_t>(this));
}
}
} else if (previous == DIRECTMANIPULATION_RUNNING) {
if (owner_->binding_handler_delegate) {
owner_->binding_handler_delegate->OnPointerPanZoomEnd(
(int32_t) reinterpret_cast<int64_t>(this));
}
// Need to reset the content transform to its original position
// so that we are ready for the next gesture.
// Use during_synthesized_reset_ flag to prevent sending reset also to the
// framework.
during_synthesized_reset_ = true;
RECT rect;
HRESULT hr = viewport->GetViewportRect(&rect);
if (FAILED(hr)) {
FML_LOG(ERROR) << "Failed to get the current viewport rect";
return E_FAIL;
}
hr = viewport->ZoomToRect(rect.left, rect.top, rect.right, rect.bottom,
false);
if (FAILED(hr)) {
FML_LOG(ERROR) << "Failed to reset the gesture using ZoomToRect";
return E_FAIL;
}
}
return S_OK;
}

HRESULT DirectManipulationEventHandler::OnViewportUpdated(
IDirectManipulationViewport* viewport) {
return S_OK;
}

HRESULT DirectManipulationEventHandler::OnContentUpdated(
IDirectManipulationViewport* viewport,
IDirectManipulationContent* content) {
float transform[6];
HRESULT hr = content->GetContentTransform(transform, ARRAYSIZE(transform));
if (FAILED(hr)) {
FML_LOG(ERROR) << "GetContentTransform failed";
return S_OK;
}
if (!during_synthesized_reset_) {
// DirectManipulation provides updates with very high precision. If the user
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Have we tested this part yet?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Added a test for it

// holds their fingers steady on a trackpad, DirectManipulation sends
// jittery updates. This calculation will reduce the precision of the scale
// value of the event to avoid jitter.
const int mantissa_bits_chop = 2;
const float factor = (1 << mantissa_bits_chop) + 1;
float c = factor * transform[0];
float scale = c - (c - transform[0]);
float pan_x = transform[4];
float pan_y = transform[5];
if (owner_->binding_handler_delegate) {
owner_->binding_handler_delegate->OnPointerPanZoomUpdate(
(int32_t) reinterpret_cast<int64_t>(this), pan_x, pan_y, scale, 0);
}
}
return S_OK;
}

HRESULT DirectManipulationEventHandler::OnInteraction(
IDirectManipulationViewport2* viewport,
DIRECTMANIPULATION_INTERACTION_TYPE interaction) {
return S_OK;
}

ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::AddRef() {
RefCountedThreadSafe::AddRef();
return 0;
}

ULONG STDMETHODCALLTYPE DirectManipulationEventHandler::Release() {
RefCountedThreadSafe::Release();
return 0;
}

DirectManipulationOwner::DirectManipulationOwner(WindowWin32* window)
: window_(window) {}

int DirectManipulationOwner::Init(unsigned int width, unsigned int height) {
RETURN_IF_FAILED(CoCreateInstance(CLSID_DirectManipulationManager, nullptr,
CLSCTX_INPROC_SERVER,
IID_IDirectManipulationManager, &manager_));
RETURN_IF_FAILED(manager_->GetUpdateManager(
IID_IDirectManipulationUpdateManager, &updateManager_));
RETURN_IF_FAILED(manager_->CreateViewport(nullptr, window_->GetWindowHandle(),
IID_IDirectManipulationViewport,
&viewport_));
DIRECTMANIPULATION_CONFIGURATION configuration =
DIRECTMANIPULATION_CONFIGURATION_INTERACTION |
DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_X |
DIRECTMANIPULATION_CONFIGURATION_TRANSLATION_Y |
DIRECTMANIPULATION_CONFIGURATION_SCALING;
RETURN_IF_FAILED(viewport_->ActivateConfiguration(configuration));
RETURN_IF_FAILED(viewport_->SetViewportOptions(
DIRECTMANIPULATION_VIEWPORT_OPTIONS_MANUALUPDATE));
handler_ = fml::MakeRefCounted<DirectManipulationEventHandler>(this);
RETURN_IF_FAILED(viewport_->AddEventHandler(
window_->GetWindowHandle(), handler_.get(), &viewportHandlerCookie_));
RECT rect = {0, 0, (LONG)width, (LONG)height};
RETURN_IF_FAILED(viewport_->SetViewportRect(&rect));
RETURN_IF_FAILED(manager_->Activate(window_->GetWindowHandle()));
RETURN_IF_FAILED(viewport_->Enable());
RETURN_IF_FAILED(updateManager_->Update(nullptr));
return 0;
}

void DirectManipulationOwner::ResizeViewport(unsigned int width,
unsigned int height) {
if (viewport_) {
RECT rect = {0, 0, (LONG)width, (LONG)height};
WARN_IF_FAILED(viewport_->SetViewportRect(&rect));
}
}

void DirectManipulationOwner::Destroy() {
if (handler_) {
handler_->owner_ = nullptr;
}

if (viewport_) {
WARN_IF_FAILED(viewport_->Disable());
WARN_IF_FAILED(viewport_->Disable());
WARN_IF_FAILED(viewport_->RemoveEventHandler(viewportHandlerCookie_));
WARN_IF_FAILED(viewport_->Abandon());
}

if (window_ && manager_) {
WARN_IF_FAILED(manager_->Deactivate(window_->GetWindowHandle()));
}

handler_ = nullptr;
viewport_ = nullptr;
updateManager_ = nullptr;
manager_ = nullptr;
window_ = nullptr;
}

void DirectManipulationOwner::SetContact(UINT contactId) {
if (viewport_) {
viewport_->SetContact(contactId);
}
}

void DirectManipulationOwner::SetBindingHandlerDelegate(
WindowBindingHandlerDelegate* delegate) {
binding_handler_delegate = delegate;
}

void DirectManipulationOwner::Update() {
if (updateManager_) {
HRESULT hr = updateManager_->Update(nullptr);
if (FAILED(hr)) {
FML_LOG(ERROR) << "updateManager_->Update failed";
auto error = GetLastError();
FML_LOG(ERROR) << error;
LPWSTR message = nullptr;
size_t size = FormatMessageW(
FORMAT_MESSAGE_ALLOCATE_BUFFER | FORMAT_MESSAGE_FROM_SYSTEM |
FORMAT_MESSAGE_IGNORE_INSERTS,
NULL, error, MAKELANGID(LANG_NEUTRAL, SUBLANG_DEFAULT),
reinterpret_cast<LPWSTR>(&message), 0, NULL);
FML_LOG(ERROR) << message;
}
}
}

} // namespace flutter
116 changes: 116 additions & 0 deletions shell/platform/windows/direct_manipulation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,116 @@
// Copyright 2013 The Flutter 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 FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_
#define FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_

#include "flutter/fml/memory/ref_counted.h"

#include <wrl/client.h>
#include "directmanipulation.h"

namespace flutter {

class WindowWin32;
class WindowBindingHandlerDelegate;

class DirectManipulationEventHandler;

// Owner for a DirectManipulation event handler, contains the link between
// DirectManipulation and WindowBindingHandlerDelegate.
class DirectManipulationOwner {
public:
explicit DirectManipulationOwner(WindowWin32* window);
// Initialize a DirectManipulation viewport with specified width and height.
// These should match the width and height of the application window.
int Init(unsigned int width, unsigned int height);
// Resize the DirectManipulation viewport. Should be called when the
// application window is resized.
void ResizeViewport(unsigned int width, unsigned int height);
// Set the WindowBindingHandlerDelegate which will receive callbacks based on
// DirectManipulation updates.
void SetBindingHandlerDelegate(
WindowBindingHandlerDelegate* binding_handler_delegate);
// Called when DM_POINTERHITTEST occurs with an acceptable pointer type. Will
// start DirectManipulation for that interaction.
void SetContact(UINT contactId);
// Called to get updates from DirectManipulation. Should be called frequently
// to provide smooth updates.
void Update();
// Release child event handler and OS resources.
void Destroy();
// The target that should be updated when DirectManipulation provides a new
// pan/zoom transformation.
WindowBindingHandlerDelegate* binding_handler_delegate;

private:
// The window gesture input is occuring on.
WindowWin32* window_;
// Cookie needed to register child event handler with viewport.
DWORD viewportHandlerCookie_;
// Object needed for operation of the DirectManipulation API.
Microsoft::WRL::ComPtr<IDirectManipulationManager> manager_;
// Object needed for operation of the DirectManipulation API.
Microsoft::WRL::ComPtr<IDirectManipulationUpdateManager> updateManager_;
// Object needed for operation of the DirectManipulation API.
Microsoft::WRL::ComPtr<IDirectManipulationViewport> viewport_;
// Child needed for operation of the DirectManipulation API.
fml::RefPtr<DirectManipulationEventHandler> handler_;
};

// Implements DirectManipulation event handling interfaces, receives calls from
// system when gesture events occur.
class DirectManipulationEventHandler
: public fml::RefCountedThreadSafe<DirectManipulationEventHandler>,
public IDirectManipulationViewportEventHandler,
public IDirectManipulationInteractionEventHandler {
friend class DirectManipulationOwner;
FML_FRIEND_REF_COUNTED_THREAD_SAFE(DirectManipulationEventHandler);
FML_FRIEND_MAKE_REF_COUNTED(DirectManipulationEventHandler);

public:
explicit DirectManipulationEventHandler(DirectManipulationOwner* owner)
: owner_(owner) {}

// |IUnknown|
STDMETHODIMP QueryInterface(REFIID iid, void** ppv) override;

// |IUnknown|
ULONG STDMETHODCALLTYPE AddRef() override;

// |IUnknown|
ULONG STDMETHODCALLTYPE Release() override;

// |IDirectManipulationViewportEventHandler|
HRESULT STDMETHODCALLTYPE
OnViewportStatusChanged(IDirectManipulationViewport* viewport,
DIRECTMANIPULATION_STATUS current,
DIRECTMANIPULATION_STATUS previous) override;

// |IDirectManipulationViewportEventHandler|
HRESULT STDMETHODCALLTYPE
OnViewportUpdated(IDirectManipulationViewport* viewport) override;

// |IDirectManipulationViewportEventHandler|
HRESULT STDMETHODCALLTYPE
OnContentUpdated(IDirectManipulationViewport* viewport,
IDirectManipulationContent* content) override;

// |IDirectManipulationInteractionEventHandler|
HRESULT STDMETHODCALLTYPE
OnInteraction(IDirectManipulationViewport2* viewport,
DIRECTMANIPULATION_INTERACTION_TYPE interaction) override;

private:
// Parent object, used to store the target for gesture event updates.
DirectManipulationOwner* owner_;
// We need to reset some parts of DirectManipulation after each gesture
// A flag is needed to ensure that false events created as the reset occurs
// are not sent to the flutter framework.
bool during_synthesized_reset_ = false;
};

} // namespace flutter

#endif // FLUTTER_SHELL_PLATFORM_WINDOWS_DIRECT_MANIPULATION_H_
Loading