The Darwin FML backend. (flutter/engine#3490)

This commit is contained in:
Chinmay Garde
2017-03-20 15:47:40 -07:00
committed by GitHub
parent d41b5c8298
commit db14a133e2
5 changed files with 187 additions and 0 deletions

View File

@@ -6,6 +6,7 @@
#define FLUTTER_FML_PLATFORM_DARWIN_CF_UTILS_H_
#include <CoreFoundation/CoreFoundation.h>
#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; }

View File

@@ -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 <CoreFoundation/CoreFoundation.h>
#include <atomic>
#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<CFRunLoopTimerRef> delayed_wake_timer_;
CFRef<CFRunLoopRef> 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_

View File

@@ -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 <Foundation/Foundation.h>
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<CFRunLoopTimerCallBack>(&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

View File

@@ -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_

View File

@@ -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 <Foundation/Foundation.h>
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