Skip to content

Commit 89e3958

Browse files
authored
Refactor to passing functions by const ref (flutter#13975)
Moved our code to passing functions by const ref
1 parent 11580eb commit 89e3958

File tree

81 files changed

+213
-187
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

81 files changed

+213
-187
lines changed

flow/raster_cache.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -92,13 +92,14 @@ static bool IsPictureWorthRasterizing(SkPicture* picture,
9292
return picture->approximateOpCount() > 5;
9393
}
9494

95+
/// @note Procedure doesn't copy all closures.
9596
static RasterCacheResult Rasterize(
9697
GrContext* context,
9798
const SkMatrix& ctm,
9899
SkColorSpace* dst_color_space,
99100
bool checkerboard,
100101
const SkRect& logical_rect,
101-
std::function<void(SkCanvas*)> draw_function) {
102+
const std::function<void(SkCanvas*)>& draw_function) {
102103
TRACE_EVENT0("flutter", "RasterCachePopulate");
103104
SkIRect cache_rect = RasterCache::GetDeviceBounds(logical_rect, ctm);
104105

flow/scene_update_context.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -37,7 +37,7 @@ class SceneUpdateContext {
3737
virtual SkISize GetSize() const = 0;
3838

3939
virtual void SignalWritesFinished(
40-
std::function<void(void)> on_writes_committed) = 0;
40+
const std::function<void(void)>& on_writes_committed) = 0;
4141

4242
virtual scenic::Image* GetImage() = 0;
4343

flow/view_holder.cc

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -51,7 +51,7 @@ namespace flutter {
5151
void ViewHolder::Create(zx_koid_t id,
5252
fml::RefPtr<fml::TaskRunner> ui_task_runner,
5353
fuchsia::ui::views::ViewHolderToken view_holder_token,
54-
BindCallback on_bind_callback) {
54+
const BindCallback& on_bind_callback) {
5555
// This GPU thread contains at least 1 ViewHolder. Initialize the per-thread
5656
// bindings.
5757
if (tls_view_holder_bindings.get() == nullptr) {
@@ -64,7 +64,7 @@ void ViewHolder::Create(zx_koid_t id,
6464

6565
auto view_holder = std::make_unique<ViewHolder>(std::move(ui_task_runner),
6666
std::move(view_holder_token),
67-
std::move(on_bind_callback));
67+
on_bind_callback);
6868
bindings->emplace(id, std::move(view_holder));
6969
}
7070

@@ -91,10 +91,10 @@ ViewHolder* ViewHolder::FromId(zx_koid_t id) {
9191

9292
ViewHolder::ViewHolder(fml::RefPtr<fml::TaskRunner> ui_task_runner,
9393
fuchsia::ui::views::ViewHolderToken view_holder_token,
94-
BindCallback on_bind_callback)
94+
const BindCallback& on_bind_callback)
9595
: ui_task_runner_(std::move(ui_task_runner)),
9696
pending_view_holder_token_(std::move(view_holder_token)),
97-
pending_bind_callback_(std::move(on_bind_callback)) {
97+
pending_bind_callback_(on_bind_callback) {
9898
FML_DCHECK(ui_task_runner_);
9999
FML_DCHECK(pending_view_holder_token_.value);
100100
}

flow/view_holder.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,13 +34,13 @@ class ViewHolder {
3434
static void Create(zx_koid_t id,
3535
fml::RefPtr<fml::TaskRunner> ui_task_runner,
3636
fuchsia::ui::views::ViewHolderToken view_holder_token,
37-
BindCallback on_bind_callback);
37+
const BindCallback& on_bind_callback);
3838
static void Destroy(zx_koid_t id);
3939
static ViewHolder* FromId(zx_koid_t id);
4040

4141
ViewHolder(fml::RefPtr<fml::TaskRunner> ui_task_runner,
4242
fuchsia::ui::views::ViewHolderToken view_holder_token,
43-
BindCallback on_bind_callback);
43+
const BindCallback& on_bind_callback);
4444
~ViewHolder() = default;
4545

4646
// Sets the properties/opacity of the child view by issuing a Scenic command.

fml/closure.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ using closure = std::function<void()>;
3131
///
3232
class ScopedCleanupClosure {
3333
public:
34-
ScopedCleanupClosure(fml::closure closure) : closure_(closure) {}
34+
ScopedCleanupClosure(const fml::closure& closure) : closure_(closure) {}
3535

3636
~ScopedCleanupClosure() {
3737
if (closure_) {

fml/concurrent_message_loop.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -43,7 +43,7 @@ std::shared_ptr<ConcurrentTaskRunner> ConcurrentMessageLoop::GetTaskRunner() {
4343
return std::make_shared<ConcurrentTaskRunner>(weak_from_this());
4444
}
4545

46-
void ConcurrentMessageLoop::PostTask(fml::closure task) {
46+
void ConcurrentMessageLoop::PostTask(const fml::closure& task) {
4747
if (!task) {
4848
return;
4949
}
@@ -107,7 +107,7 @@ ConcurrentTaskRunner::ConcurrentTaskRunner(
107107

108108
ConcurrentTaskRunner::~ConcurrentTaskRunner() = default;
109109

110-
void ConcurrentTaskRunner::PostTask(fml::closure task) {
110+
void ConcurrentTaskRunner::PostTask(const fml::closure& task) {
111111
if (!task) {
112112
return;
113113
}

fml/concurrent_message_loop.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -44,7 +44,7 @@ class ConcurrentMessageLoop
4444

4545
void WorkerMain();
4646

47-
void PostTask(fml::closure task);
47+
void PostTask(const fml::closure& task);
4848

4949
FML_DISALLOW_COPY_AND_ASSIGN(ConcurrentMessageLoop);
5050
};
@@ -55,7 +55,7 @@ class ConcurrentTaskRunner {
5555

5656
~ConcurrentTaskRunner();
5757

58-
void PostTask(fml::closure task);
58+
void PostTask(const fml::closure& task);
5959

6060
private:
6161
friend ConcurrentMessageLoop;

fml/delayed_task.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -9,9 +9,9 @@
99
namespace fml {
1010

1111
DelayedTask::DelayedTask(size_t order,
12-
fml::closure task,
12+
const fml::closure& task,
1313
fml::TimePoint target_time)
14-
: order_(order), task_(std::move(task)), target_time_(target_time) {}
14+
: order_(order), task_(task), target_time_(target_time) {}
1515

1616
DelayedTask::DelayedTask(const DelayedTask& other) = default;
1717

fml/delayed_task.h

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,9 @@ namespace fml {
1414

1515
class DelayedTask {
1616
public:
17-
DelayedTask(size_t order, fml::closure task, fml::TimePoint target_time);
17+
DelayedTask(size_t order,
18+
const fml::closure& task,
19+
fml::TimePoint target_time);
1820

1921
DelayedTask(const DelayedTask& other);
2022

fml/file.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -62,7 +62,7 @@ ScopedTemporaryDirectory::~ScopedTemporaryDirectory() {
6262
}
6363

6464
bool VisitFilesRecursively(const fml::UniqueFD& directory,
65-
FileVisitor visitor) {
65+
const FileVisitor& visitor) {
6666
FileVisitor recursive_visitor = [&recursive_visitor, &visitor](
6767
const UniqueFD& directory,
6868
const std::string& filename) {

fml/file.h

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,8 @@ using FileVisitor = std::function<bool(const fml::UniqueFD& directory,
107107
/// use our helper method `VisitFilesRecursively`.
108108
///
109109
/// @see `VisitFilesRecursively`.
110-
bool VisitFiles(const fml::UniqueFD& directory, FileVisitor visitor);
110+
/// @note Procedure doesn't copy all closures.
111+
bool VisitFiles(const fml::UniqueFD& directory, const FileVisitor& visitor);
111112

112113
/// Recursively call `visitor` on all files inside the `directory`. Return false
113114
/// if and only if the visitor returns false during the traversal.
@@ -119,7 +120,9 @@ bool VisitFiles(const fml::UniqueFD& directory, FileVisitor visitor);
119120
/// compute the relative path between the root directory and the visited file.
120121
///
121122
/// @see `VisitFiles`.
122-
bool VisitFilesRecursively(const fml::UniqueFD& directory, FileVisitor visitor);
123+
/// @note Procedure doesn't copy all closures.
124+
bool VisitFilesRecursively(const fml::UniqueFD& directory,
125+
const FileVisitor& visitor);
123126

124127
class ScopedTemporaryDirectory {
125128
public:

fml/mapping.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -85,7 +85,7 @@ const uint8_t* DataMapping::GetMapping() const {
8585

8686
NonOwnedMapping::NonOwnedMapping(const uint8_t* data,
8787
size_t size,
88-
ReleaseProc release_proc)
88+
const ReleaseProc& release_proc)
8989
: data_(data), size_(size), release_proc_(release_proc) {}
9090

9191
NonOwnedMapping::~NonOwnedMapping() {

fml/mapping.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -107,7 +107,7 @@ class NonOwnedMapping final : public Mapping {
107107
using ReleaseProc = std::function<void(const uint8_t* data, size_t size)>;
108108
NonOwnedMapping(const uint8_t* data,
109109
size_t size,
110-
ReleaseProc release_proc = nullptr);
110+
const ReleaseProc& release_proc = nullptr);
111111

112112
~NonOwnedMapping() override;
113113

fml/message_loop.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -61,7 +61,7 @@ fml::RefPtr<MessageLoopImpl> MessageLoop::GetLoopImpl() const {
6161
return loop_;
6262
}
6363

64-
void MessageLoop::AddTaskObserver(intptr_t key, fml::closure callback) {
64+
void MessageLoop::AddTaskObserver(intptr_t key, const fml::closure& callback) {
6565
loop_->AddTaskObserver(key, callback);
6666
}
6767

fml/message_loop.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -24,7 +24,7 @@ class MessageLoop {
2424

2525
void Terminate();
2626

27-
void AddTaskObserver(intptr_t key, fml::closure callback);
27+
void AddTaskObserver(intptr_t key, const fml::closure& callback);
2828

2929
void RemoveTaskObserver(intptr_t key);
3030

fml/message_loop_impl.cc

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -50,7 +50,8 @@ MessageLoopImpl::~MessageLoopImpl() {
5050
task_queue_->Dispose(queue_id_);
5151
}
5252

53-
void MessageLoopImpl::PostTask(fml::closure task, fml::TimePoint target_time) {
53+
void MessageLoopImpl::PostTask(const fml::closure& task,
54+
fml::TimePoint target_time) {
5455
FML_DCHECK(task != nullptr);
5556
FML_DCHECK(task != nullptr);
5657
if (terminated_) {
@@ -61,7 +62,8 @@ void MessageLoopImpl::PostTask(fml::closure task, fml::TimePoint target_time) {
6162
task_queue_->RegisterTask(queue_id_, task, target_time);
6263
}
6364

64-
void MessageLoopImpl::AddTaskObserver(intptr_t key, fml::closure callback) {
65+
void MessageLoopImpl::AddTaskObserver(intptr_t key,
66+
const fml::closure& callback) {
6567
FML_DCHECK(callback != nullptr);
6668
FML_DCHECK(MessageLoop::GetCurrent().GetLoopImpl().get() == this)
6769
<< "Message loop task observer must be added on the same thread as the "

fml/message_loop_impl.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -34,9 +34,9 @@ class MessageLoopImpl : public Wakeable,
3434

3535
virtual void Terminate() = 0;
3636

37-
void PostTask(fml::closure task, fml::TimePoint target_time);
37+
void PostTask(const fml::closure& task, fml::TimePoint target_time);
3838

39-
void AddTaskObserver(intptr_t key, fml::closure callback);
39+
void AddTaskObserver(intptr_t key, const fml::closure& callback);
4040

4141
void RemoveTaskObserver(intptr_t key);
4242

fml/message_loop_task_queues.cc

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -75,13 +75,13 @@ void MessageLoopTaskQueues::DisposeTasks(TaskQueueId queue_id) {
7575
}
7676

7777
void MessageLoopTaskQueues::RegisterTask(TaskQueueId queue_id,
78-
fml::closure task,
78+
const fml::closure& task,
7979
fml::TimePoint target_time) {
8080
std::scoped_lock queue_lock(GetMutex(queue_id));
8181

8282
size_t order = order_++;
8383
const auto& queue_entry = queue_entries_[queue_id];
84-
queue_entry->delayed_tasks.push({order, std::move(task), target_time});
84+
queue_entry->delayed_tasks.push({order, task, target_time});
8585
TaskQueueId loop_to_wake = queue_id;
8686
if (queue_entry->subsumed_by != _kUnmerged) {
8787
loop_to_wake = queue_entry->subsumed_by;
@@ -157,7 +157,7 @@ size_t MessageLoopTaskQueues::GetNumPendingTasks(TaskQueueId queue_id) const {
157157

158158
void MessageLoopTaskQueues::AddTaskObserver(TaskQueueId queue_id,
159159
intptr_t key,
160-
fml::closure callback) {
160+
const fml::closure& callback) {
161161
std::scoped_lock queue_lock(GetMutex(queue_id));
162162

163163
FML_DCHECK(callback != nullptr) << "Observer callback must be non-null.";

fml/message_loop_task_queues.h

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -78,7 +78,7 @@ class MessageLoopTaskQueues
7878
// Tasks methods.
7979

8080
void RegisterTask(TaskQueueId queue_id,
81-
fml::closure task,
81+
const fml::closure& task,
8282
fml::TimePoint target_time);
8383

8484
bool HasPendingTasks(TaskQueueId queue_id) const;
@@ -93,7 +93,7 @@ class MessageLoopTaskQueues
9393

9494
void AddTaskObserver(TaskQueueId queue_id,
9595
intptr_t key,
96-
fml::closure callback);
96+
const fml::closure& callback);
9797

9898
void RemoveTaskObserver(TaskQueueId queue_id, intptr_t key);
9999

fml/platform/posix/file_posix.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -222,7 +222,7 @@ bool WriteAtomically(const fml::UniqueFD& base_directory,
222222
base_directory.get(), file_name) == 0;
223223
}
224224

225-
bool VisitFiles(const fml::UniqueFD& directory, FileVisitor visitor) {
225+
bool VisitFiles(const fml::UniqueFD& directory, const FileVisitor& visitor) {
226226
fml::UniqueFD dup_fd(dup(directory.get()));
227227
if (!dup_fd.is_valid()) {
228228
FML_DLOG(ERROR) << "Can't dup the directory fd. Error: " << strerror(errno);

fml/platform/win/file_win.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -399,7 +399,7 @@ bool WriteAtomically(const fml::UniqueFD& base_directory,
399399
return true;
400400
}
401401

402-
bool VisitFiles(const fml::UniqueFD& directory, FileVisitor visitor) {
402+
bool VisitFiles(const fml::UniqueFD& directory, const FileVisitor& visitor) {
403403
std::string search_pattern = GetFullHandlePath(directory) + "\\*";
404404
WIN32_FIND_DATA find_file_data;
405405
HANDLE find_handle = ::FindFirstFile(

fml/task_runner.cc

Lines changed: 8 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -20,17 +20,18 @@ TaskRunner::TaskRunner(fml::RefPtr<MessageLoopImpl> loop)
2020

2121
TaskRunner::~TaskRunner() = default;
2222

23-
void TaskRunner::PostTask(fml::closure task) {
24-
loop_->PostTask(std::move(task), fml::TimePoint::Now());
23+
void TaskRunner::PostTask(const fml::closure& task) {
24+
loop_->PostTask(task, fml::TimePoint::Now());
2525
}
2626

27-
void TaskRunner::PostTaskForTime(fml::closure task,
27+
void TaskRunner::PostTaskForTime(const fml::closure& task,
2828
fml::TimePoint target_time) {
29-
loop_->PostTask(std::move(task), target_time);
29+
loop_->PostTask(task, target_time);
3030
}
3131

32-
void TaskRunner::PostDelayedTask(fml::closure task, fml::TimeDelta delay) {
33-
loop_->PostTask(std::move(task), fml::TimePoint::Now() + delay);
32+
void TaskRunner::PostDelayedTask(const fml::closure& task,
33+
fml::TimeDelta delay) {
34+
loop_->PostTask(task, fml::TimePoint::Now() + delay);
3435
}
3536

3637
TaskQueueId TaskRunner::GetTaskQueueId() {
@@ -62,7 +63,7 @@ bool TaskRunner::RunsTasksOnCurrentThread() {
6263
}
6364

6465
void TaskRunner::RunNowOrPostTask(fml::RefPtr<fml::TaskRunner> runner,
65-
fml::closure task) {
66+
const fml::closure& task) {
6667
FML_DCHECK(runner);
6768
if (runner->RunsTasksOnCurrentThread()) {
6869
task();

fml/task_runner.h

Lines changed: 5 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -20,18 +20,19 @@ class TaskRunner : public fml::RefCountedThreadSafe<TaskRunner> {
2020
public:
2121
virtual ~TaskRunner();
2222

23-
virtual void PostTask(fml::closure task);
23+
virtual void PostTask(const fml::closure& task);
2424

25-
virtual void PostTaskForTime(fml::closure task, fml::TimePoint target_time);
25+
virtual void PostTaskForTime(const fml::closure& task,
26+
fml::TimePoint target_time);
2627

27-
virtual void PostDelayedTask(fml::closure task, fml::TimeDelta delay);
28+
virtual void PostDelayedTask(const fml::closure& task, fml::TimeDelta delay);
2829

2930
virtual bool RunsTasksOnCurrentThread();
3031

3132
virtual TaskQueueId GetTaskQueueId();
3233

3334
static void RunNowOrPostTask(fml::RefPtr<fml::TaskRunner> runner,
34-
fml::closure task);
35+
const fml::closure& task);
3536

3637
protected:
3738
TaskRunner(fml::RefPtr<MessageLoopImpl> loop);

lib/ui/painting/image_decoder.cc

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -188,7 +188,8 @@ static SkiaGPUObject<SkImage> UploadRasterImage(
188188
return {texture_image, queue};
189189
}
190190

191-
void ImageDecoder::Decode(ImageDescriptor descriptor, ImageResult callback) {
191+
void ImageDecoder::Decode(ImageDescriptor descriptor,
192+
const ImageResult& callback) {
192193
TRACE_EVENT0("flutter", __FUNCTION__);
193194
fml::tracing::TraceFlow flow(__FUNCTION__);
194195

lib/ui/painting/image_decoder.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,7 @@ class ImageDecoder {
5555
// concurrently. Texture upload is done on the IO thread and the result
5656
// returned back on the UI thread. On error, the texture is null but the
5757
// callback is guaranteed to return on the UI thread.
58-
void Decode(ImageDescriptor descriptor, ImageResult result);
58+
void Decode(ImageDescriptor descriptor, const ImageResult& result);
5959

6060
fml::WeakPtr<ImageDecoder> GetWeakPtr() const;
6161

0 commit comments

Comments
 (0)