diff --git a/engine/src/flutter/fml/platform/darwin/cf_utils.h b/engine/src/flutter/fml/platform/darwin/cf_utils.h index 18fb0d0608..309f1105f2 100644 --- a/engine/src/flutter/fml/platform/darwin/cf_utils.h +++ b/engine/src/flutter/fml/platform/darwin/cf_utils.h @@ -6,6 +6,7 @@ #define FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_ #include + #include "lib/ftl/macros.h" namespace fml { @@ -24,6 +25,14 @@ class CFRef { instance_ = nullptr; } + void Reset(T instance) { + if (instance_ != nullptr) { + CFRelease(instance_); + } + + instance_ = instance; + } + operator T() const { return instance_; } operator bool() const { return instance_ != nullptr; } diff --git a/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h new file mode 100644 index 0000000000..6702b056bf --- /dev/null +++ b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.h @@ -0,0 +1,43 @@ +// 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. + +#ifndef FLUTTER_FML_PLATFORM_DARWIN_MESSAGE_LOOP_DARWIN_H_ +#define FLUTTER_FML_PLATFORM_DARWIN_MESSAGE_LOOP_DARWIN_H_ + +#include + +#include + +#include "flutter/fml/message_loop_impl.h" +#include "flutter/fml/platform/darwin/cf_utils.h" +#include "lib/ftl/macros.h" + +namespace fml { + +class MessageLoopDarwin : public MessageLoopImpl { + private: + std::atomic_bool running_; + CFRef delayed_wake_timer_; + CFRef loop_; + + MessageLoopDarwin(); + + ~MessageLoopDarwin() override; + + void Run() override; + + void Terminate() override; + + void WakeUp(ftl::TimePoint time_point) override; + + static void OnTimerFire(CFRunLoopTimerRef timer, MessageLoopDarwin* loop); + + FRIEND_MAKE_REF_COUNTED(MessageLoopDarwin); + FRIEND_REF_COUNTED_THREAD_SAFE(MessageLoopDarwin); + FTL_DISALLOW_COPY_AND_ASSIGN(MessageLoopDarwin); +}; + +} // namespace fml + +#endif // FLUTTER_FML_PLATFORM_DARWIN_MESSAGE_LOOP_DARWIN_H_ diff --git a/engine/src/flutter/fml/platform/darwin/message_loop_darwin.mm b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.mm new file mode 100644 index 0000000000..6e86ef233a --- /dev/null +++ b/engine/src/flutter/fml/platform/darwin/message_loop_darwin.mm @@ -0,0 +1,79 @@ +// 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/platform/darwin/message_loop_darwin.h" + +#include + +namespace fml { + +static constexpr CFTimeInterval kDistantFuture = 1.0e10; + +MessageLoopDarwin::MessageLoopDarwin() + : running_(false), loop_((CFRunLoopRef)CFRetain(CFRunLoopGetCurrent())) { + FTL_DCHECK(loop_ != nullptr); + + // Setup the delayed wake source. + CFRunLoopTimerContext timer_context = { + .info = this, + }; + delayed_wake_timer_.Reset(CFRunLoopTimerCreate( + kCFAllocatorDefault, kDistantFuture /* fire date */, + HUGE_VAL /* interval */, 0 /* flags */, 0 /* order */, + reinterpret_cast(&MessageLoopDarwin::OnTimerFire) + /* callout */, + &timer_context /* context */)); + FTL_DCHECK(delayed_wake_timer_ != nullptr); + CFRunLoopAddTimer(loop_, delayed_wake_timer_, kCFRunLoopCommonModes); +} + +MessageLoopDarwin::~MessageLoopDarwin() { + CFRunLoopTimerInvalidate(delayed_wake_timer_); + CFRunLoopRemoveTimer(loop_, delayed_wake_timer_, kCFRunLoopCommonModes); +} + +void MessageLoopDarwin::Run() { + FTL_DCHECK(loop_ == CFRunLoopGetCurrent()); + + running_ = true; + + while (running_) { + @autoreleasepool { + CFRunLoopRunResult result = + CFRunLoopRunInMode(kCFRunLoopDefaultMode, kDistantFuture, YES); + if (result == kCFRunLoopRunStopped || result == kCFRunLoopRunFinished) { + // This handles the case where the loop is terminated using + // CoreFoundation APIs. + @autoreleasepool { + RunExpiredTasksNow(); + } + running_ = false; + } + } + } +} + +void MessageLoopDarwin::Terminate() { + running_ = false; + CFRunLoopStop(loop_); +} + +void MessageLoopDarwin::WakeUp(ftl::TimePoint time_point) { + // Rearm the timer. The time bases used by CoreFoundation and FTL are + // different and must be accounted for. + CFRunLoopTimerSetNextFireDate( + delayed_wake_timer_, + CFAbsoluteTimeGetCurrent() + + (time_point - ftl::TimePoint::Now()).ToSecondsF()); +} + +void MessageLoopDarwin::OnTimerFire(CFRunLoopTimerRef timer, + MessageLoopDarwin* loop) { + @autoreleasepool { + // RunExpiredTasksNow rearms the timer as appropriate via a call to WakeUp. + loop->RunExpiredTasksNow(); + } +} + +} // namespace fml diff --git a/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h new file mode 100644 index 0000000000..033e15a608 --- /dev/null +++ b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.h @@ -0,0 +1,31 @@ +// 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. + +#ifndef FLUTTER_FML_PLATFORM_DARWIN_RESOURCE_MAPPING_DARWIN_H_ +#define FLUTTER_FML_PLATFORM_DARWIN_RESOURCE_MAPPING_DARWIN_H_ + +#include "flutter/fml/mapping.h" +#include "lib/ftl/macros.h" + +namespace fml { + +class ResourceMappingDarwin : public Mapping { + public: + ResourceMappingDarwin(const std::string& resource); + + ~ResourceMappingDarwin() override; + + size_t GetSize() const override; + + const uint8_t* GetMapping() const override; + + private: + FileMapping actual_; + + FTL_DISALLOW_COPY_AND_ASSIGN(ResourceMappingDarwin); +}; + +} // namespace fml + +#endif // FLUTTER_FML_PLATFORM_DARWIN_RESOURCE_MAPPING_DARWIN_H_ diff --git a/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.mm b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.mm new file mode 100644 index 0000000000..03d31840f2 --- /dev/null +++ b/engine/src/flutter/fml/platform/darwin/resource_mapping_darwin.mm @@ -0,0 +1,25 @@ +// 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/platform/darwin/resource_mapping_darwin.h" + +#include + +namespace fml { + +ResourceMappingDarwin::ResourceMappingDarwin(const std::string& resource) + : actual_([[[NSBundle mainBundle] pathForResource:@(resource.c_str()) + ofType:nil] UTF8String]) {} + +ResourceMappingDarwin::~ResourceMappingDarwin() = default; + +size_t ResourceMappingDarwin::GetSize() const { + return actual_.GetSize(); +} + +const uint8_t* ResourceMappingDarwin::GetMapping() const { + return actual_.GetMapping(); +} + +} // namespace fml