Updated fml to build on Windows. (flutter/engine#4415)

This commit is contained in:
Ben Konyi
2017-12-08 10:40:10 -08:00
committed by GitHub
parent adf6949757
commit 5ff8528682
8 changed files with 149 additions and 35 deletions

View File

@@ -6,7 +6,6 @@ source_set("fml") {
sources = [
"icu_util.cc",
"icu_util.h",
"mapping.cc",
"mapping.h",
"memory/weak_ptr.h",
"memory/weak_ptr_internal.cc",
@@ -98,10 +97,15 @@ source_set("fml") {
if (is_win) {
sources += [
"platform/win/mapping_win.cc",
"platform/win/message_loop_win.cc",
"platform/win/message_loop_win.h",
"platform/win/paths_win.cc",
]
} else {
sources += [
"platform/posix/mapping_posix.cc",
]
}
}

View File

@@ -16,6 +16,12 @@
namespace fml {
namespace icu {
#if OS_WIN
static constexpr char kPathSeparator = '\\';
#else
static constexpr char kPathSeparator = '/';
#endif
static constexpr char kIcuDataFileName[] = "icudtl.dat";
class ICUContext {
@@ -53,8 +59,8 @@ class ICUContext {
// FIXME(chinmaygarde): There is no Path::Join in FXL. So a non-portable
// version is used here. Patch FXL and update.
auto file = std::make_unique<FileMapping>(directory.second + "/" +
kIcuDataFileName);
auto file = std::make_unique<FileMapping>(
directory.second + kPathSeparator + kIcuDataFileName);
if (file->GetSize() != 0) {
mapping_ = std::move(file);
return true;

View File

@@ -5,8 +5,15 @@
#ifndef FLUTTER_FML_MAPPING_H_
#define FLUTTER_FML_MAPPING_H_
#include <memory>
#include <string>
#include "lib/fxl/build_config.h"
#if OS_WIN
#include <windows.h>
#endif
#include "lib/fxl/files/unique_fd.h"
#include "lib/fxl/macros.h"
@@ -34,7 +41,10 @@ class FileMapping : public Mapping {
public:
FileMapping(const std::string& path);
// fxl::UniqueFD isn't supported for Windows handles.
#if !OS_WIN
FileMapping(const fxl::UniqueFD& fd);
#endif
~FileMapping() override;
@@ -46,6 +56,10 @@ class FileMapping : public Mapping {
size_t size_;
uint8_t* mapping_;
#if OS_WIN
HANDLE mapping_handle_;
#endif
FXL_DISALLOW_COPY_AND_ASSIGN(FileMapping);
};

View File

@@ -75,7 +75,7 @@ TEST(MessageLoop, NonDelayedTasksAreRunInOrder) {
auto& loop = fml::MessageLoop::GetCurrent();
size_t current = 0;
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTask([&terminated, i, &current]() {
loop.GetTaskRunner()->PostTask([&terminated, i, &current, count]() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
@@ -105,7 +105,7 @@ TEST(MessageLoop, DelayedTasksAtSameTimeAreRunInOrder) {
fxl::TimePoint::Now() + fxl::TimeDelta::FromMilliseconds(2);
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTaskForTime(
[&terminated, i, &current]() {
[&terminated, i, &current, count]() {
ASSERT_EQ(current, i);
current++;
if (count == i + 1) {
@@ -187,13 +187,13 @@ TEST(MessageLoop, TIME_SENSITIVE(SingleDelayedTaskForTime)) {
TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) {
const auto count = 10;
int checked = false;
std::thread thread([&checked]() {
std::thread thread([&checked, count]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = 0 + 2; target_ms < count + 2; target_ms++) {
auto begin = fxl::TimePoint::Now();
loop.GetTaskRunner()->PostDelayedTask(
[begin, target_ms, &checked]() {
[begin, target_ms, &checked, count]() {
auto delta = fxl::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
@@ -214,13 +214,13 @@ TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithIncreasingDeltas)) {
TEST(MessageLoop, TIME_SENSITIVE(MultipleDelayedTasksWithDecreasingDeltas)) {
const auto count = 10;
int checked = false;
std::thread thread([&checked]() {
std::thread thread([&checked, count]() {
fml::MessageLoop::EnsureInitializedForCurrentThread();
auto& loop = fml::MessageLoop::GetCurrent();
for (int target_ms = count + 2; target_ms > 0 + 2; target_ms--) {
auto begin = fxl::TimePoint::Now();
loop.GetTaskRunner()->PostDelayedTask(
[begin, target_ms, &checked]() {
[begin, target_ms, &checked, count]() {
auto delta = fxl::TimePoint::Now() - begin;
auto ms = delta.ToMillisecondsF();
ASSERT_GE(ms, target_ms - 2);
@@ -263,9 +263,9 @@ TEST(MessageLoop, TaskObserverFire) {
auto& loop = fml::MessageLoop::GetCurrent();
size_t task_count = 0;
size_t obs_count = 0;
CustomTaskObserver obs([&obs_count]() { obs_count++; });
CustomTaskObserver obs([&obs_count, count]() { obs_count++; });
for (size_t i = 0; i < count; i++) {
loop.GetTaskRunner()->PostTask([&terminated, i, &task_count]() {
loop.GetTaskRunner()->PostTask([&terminated, i, &task_count, count]() {
ASSERT_EQ(task_count, i);
task_count++;
if (count == i + 1) {

View File

@@ -0,0 +1,84 @@
// Copyright 2017 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 "flutter/fml/mapping.h"
#include <fcntl.h>
#include <type_traits>
#include "lib/fxl/build_config.h"
#include <io.h>
#include <windows.h>
using PlatformResourceMapping = fml::FileMapping;
namespace fml {
Mapping::Mapping() = default;
Mapping::~Mapping() = default;
bool PlatformHasResourcesBundle() {
return !std::is_same<PlatformResourceMapping, FileMapping>::value;
}
std::unique_ptr<Mapping> GetResourceMapping(const std::string& resource_name) {
return std::make_unique<PlatformResourceMapping>(resource_name);
}
FileMapping::FileMapping(const std::string& path)
: size_(0), mapping_(nullptr) {
HANDLE file_handle_ =
CreateFileA(reinterpret_cast<LPCSTR>(path.c_str()), GENERIC_READ,
FILE_SHARE_READ, nullptr, OPEN_EXISTING,
FILE_ATTRIBUTE_NORMAL | FILE_FLAG_RANDOM_ACCESS, nullptr);
if (file_handle_ == INVALID_HANDLE_VALUE) {
return;
}
size_ = GetFileSize(file_handle_, nullptr);
if (size_ == INVALID_FILE_SIZE) {
size_ = 0;
return;
}
mapping_handle_ = CreateFileMapping(file_handle_, nullptr, PAGE_READONLY, 0,
size_, nullptr);
CloseHandle(file_handle_);
if (mapping_handle_ == INVALID_HANDLE_VALUE) {
return;
}
auto mapping = MapViewOfFile(mapping_handle_, FILE_MAP_READ, 0, 0, size_);
if (mapping == INVALID_HANDLE_VALUE) {
CloseHandle(mapping_handle_);
mapping_handle_ = INVALID_HANDLE_VALUE;
return;
}
mapping_ = static_cast<uint8_t*>(mapping);
}
FileMapping::~FileMapping() {
if (mapping_ != nullptr) {
UnmapViewOfFile(mapping_);
CloseHandle(mapping_handle_);
}
}
size_t FileMapping::GetSize() const {
return size_;
}
const uint8_t* FileMapping::GetMapping() const {
return mapping_;
}
} // namespace fml

View File

@@ -45,13 +45,13 @@
#define TRACE_EVENT_INSTANT0(category_group, name) \
::fml::tracing::TraceEventInstant0(category_group, name);
#define TRACE_FLOW_BEGIN(category, name, id, args...) \
#define TRACE_FLOW_BEGIN(category, name, id) \
::fml::tracing::TraceEventFlowBegin0(category, name, id);
#define TRACE_FLOW_STEP(category, name, id, args...) \
#define TRACE_FLOW_STEP(category, name, id) \
::fml::tracing::TraceEventFlowStep0(category, name, id);
#define TRACE_FLOW_END(category, name, id, args...) \
#define TRACE_FLOW_END(category, name, id) \
::fml::tracing::TraceEventFlowEnd0(category, name, id);
#endif // TRACE_EVENT_HIDE_MACROS

View File

@@ -265,27 +265,33 @@ void Shell::RunInPlatformViewUIThread(uintptr_t view_id,
*view_existed = false;
IteratePlatformViews(
[
view_id, // argument
assets_directory = std::move(assets_directory), // argument
main = std::move(main), // argument
packages = std::move(packages), // argument
&view_existed, // out
&dart_isolate_id, // out
&isolate_name // out
](PlatformView * view)
->bool {
if (reinterpret_cast<uintptr_t>(view) != view_id) {
// Keep looking.
return true;
}
*view_existed = true;
view->RunFromSource(assets_directory, main, packages);
*dart_isolate_id = view->engine().GetUIIsolateMainPort();
*isolate_name = view->engine().GetUIIsolateName();
// We found the requested view. Stop iterating over platform views.
return false;
});
[view_id, // argument
#if !defined(OS_WIN)
// Using std::move on const references inside lambda capture is
// not supported on Windows for some reason.
assets_directory = std::move(assets_directory), // argument
main = std::move(main), // argument
packages = std::move(packages), // argument
#else
assets_directory, // argument
main, // argument
packages, // argument
#endif
&view_existed, // out
&dart_isolate_id, // out
&isolate_name // out
](PlatformView* view) -> bool {
if (reinterpret_cast<uintptr_t>(view) != view_id) {
// Keep looking.
return true;
}
*view_existed = true;
view->RunFromSource(assets_directory, main, packages);
*dart_isolate_id = view->engine().GetUIIsolateMainPort();
*isolate_name = view->engine().GetUIIsolateName();
// We found the requested view. Stop iterating over platform views.
return false;
});
latch->Signal();
}