Skip to content
This repository was archived by the owner on Feb 25, 2025. It is now read-only.

[Impeller] Add implementations for concurrent work-queues. #35355

Merged
merged 2 commits into from
Aug 11, 2022
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
6 changes: 6 additions & 0 deletions ci/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -450,6 +450,8 @@ FILE: ../../../flutter/impeller/base/base_unittests.cc
FILE: ../../../flutter/impeller/base/comparable.cc
FILE: ../../../flutter/impeller/base/comparable.h
FILE: ../../../flutter/impeller/base/config.h
FILE: ../../../flutter/impeller/base/platform/darwin/work_queue_darwin.cc
FILE: ../../../flutter/impeller/base/platform/darwin/work_queue_darwin.h
FILE: ../../../flutter/impeller/base/promise.cc
FILE: ../../../flutter/impeller/base/promise.h
FILE: ../../../flutter/impeller/base/strings.cc
Expand All @@ -462,6 +464,10 @@ FILE: ../../../flutter/impeller/base/validation.cc
FILE: ../../../flutter/impeller/base/validation.h
FILE: ../../../flutter/impeller/base/version.cc
FILE: ../../../flutter/impeller/base/version.h
FILE: ../../../flutter/impeller/base/work_queue.cc
FILE: ../../../flutter/impeller/base/work_queue.h
FILE: ../../../flutter/impeller/base/work_queue_common.cc
FILE: ../../../flutter/impeller/base/work_queue_common.h
FILE: ../../../flutter/impeller/blobcat/blob.cc
FILE: ../../../flutter/impeller/blobcat/blob.h
FILE: ../../../flutter/impeller/blobcat/blob_library.cc
Expand Down
11 changes: 11 additions & 0 deletions impeller/base/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -24,8 +24,19 @@ impeller_component("base") {
"validation.h",
"version.cc",
"version.h",
"work_queue.cc",
"work_queue.h",
"work_queue_common.cc",
"work_queue_common.h",
]

if (is_ios || is_mac) {
sources += [
"platform/darwin/work_queue_darwin.cc",
"platform/darwin/work_queue_darwin.h",
]
}

deps = [ "//flutter/fml" ]
}

Expand Down
38 changes: 38 additions & 0 deletions impeller/base/platform/darwin/work_queue_darwin.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 "impeller/base/platform/darwin/work_queue_darwin.h"

namespace impeller {

std::shared_ptr<WorkQueueDarwin> WorkQueueDarwin::Create() {
auto queue = std::shared_ptr<WorkQueueDarwin>(new WorkQueueDarwin());
if (!queue->IsValid()) {
return nullptr;
}
return queue;
}

WorkQueueDarwin::WorkQueueDarwin()
: queue_(::dispatch_queue_create(
"io.flutter.impeller.wq",
::dispatch_queue_attr_make_with_qos_class(
DISPATCH_QUEUE_CONCURRENT_WITH_AUTORELEASE_POOL,
QOS_CLASS_USER_INITIATED,
-1))) {}

WorkQueueDarwin::~WorkQueueDarwin() = default;

bool WorkQueueDarwin::IsValid() const {
return queue_ != NULL;
}

// |WorkQueue|
void WorkQueueDarwin::PostTask(fml::closure task) {
dispatch_async(queue_, ^() {
task();
});
}

} // namespace impeller
36 changes: 36 additions & 0 deletions impeller/base/platform/darwin/work_queue_darwin.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
// 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.

#pragma once

#include <dispatch/dispatch.h>

#include <memory>

#include "flutter/fml/macros.h"
#include "impeller/base/work_queue.h"

namespace impeller {

class WorkQueueDarwin final : public WorkQueue {
public:
static std::shared_ptr<WorkQueueDarwin> Create();

// |WorkQueue|
~WorkQueueDarwin();

bool IsValid() const;

private:
dispatch_queue_t queue_ = NULL;

WorkQueueDarwin();

// |WorkQueue|
void PostTask(fml::closure task) override;

FML_DISALLOW_COPY_AND_ASSIGN(WorkQueueDarwin);
};

} // namespace impeller
13 changes: 13 additions & 0 deletions impeller/base/work_queue.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,13 @@
// 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/impeller/base/work_queue.h"

namespace impeller {

WorkQueue::WorkQueue() = default;

WorkQueue::~WorkQueue() = default;

} // namespace impeller
27 changes: 27 additions & 0 deletions impeller/base/work_queue.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,27 @@
// 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.

#pragma once

#include <memory>

#include "flutter/fml/closure.h"
#include "flutter/fml/macros.h"

namespace impeller {

class WorkQueue : public std::enable_shared_from_this<WorkQueue> {
public:
virtual ~WorkQueue();

virtual void PostTask(fml::closure task) = 0;

protected:
WorkQueue();

private:
FML_DISALLOW_COPY_AND_ASSIGN(WorkQueue);
};

} // namespace impeller
25 changes: 25 additions & 0 deletions impeller/base/work_queue_common.cc
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
// 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 "impeller/base/work_queue_common.h"

namespace impeller {

std::shared_ptr<WorkQueueCommon> WorkQueueCommon::Create() {
return std::shared_ptr<WorkQueueCommon>(new WorkQueueCommon());
}

WorkQueueCommon::WorkQueueCommon()
: loop_(fml::ConcurrentMessageLoop::Create(2u)) {}

WorkQueueCommon::~WorkQueueCommon() {
loop_->Terminate();
}

// |WorkQueue|
void WorkQueueCommon::PostTask(fml::closure task) {
loop_->GetTaskRunner()->PostTask(std::move(task));
}

} // namespace impeller
33 changes: 33 additions & 0 deletions impeller/base/work_queue_common.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
// 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.

#pragma once

#include <memory>

#include "flutter/fml/concurrent_message_loop.h"
#include "flutter/fml/macros.h"
#include "impeller/base/work_queue.h"

namespace impeller {

class WorkQueueCommon : public WorkQueue {
public:
static std::shared_ptr<WorkQueueCommon> Create();

// |WorkQueue|
~WorkQueueCommon();

private:
std::shared_ptr<fml::ConcurrentMessageLoop> loop_;

WorkQueueCommon();

// |WorkQueue|
void PostTask(fml::closure task) override;

FML_DISALLOW_COPY_AND_ASSIGN(WorkQueueCommon);
};

} // namespace impeller
22 changes: 21 additions & 1 deletion impeller/renderer/backend/gles/context_gles.cc
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@

#include "impeller/base/config.h"
#include "impeller/base/validation.h"
#include "impeller/base/work_queue_common.h"

namespace impeller {

Expand Down Expand Up @@ -52,12 +53,21 @@ ContextGLES::ContextGLES(
}
}

// Create the sampler library
// Create the sampler library.
{
sampler_library_ =
std::shared_ptr<SamplerLibraryGLES>(new SamplerLibraryGLES());
}

// Create the work queue.
{
work_queue_ = WorkQueueCommon::Create();
if (!work_queue_) {
VALIDATION_LOG << "Could not create work queue.";
return;
}
}

is_valid_ = true;
}

Expand Down Expand Up @@ -86,27 +96,37 @@ bool ContextGLES::IsValid() const {
return is_valid_;
}

// |Context|
std::shared_ptr<Allocator> ContextGLES::GetResourceAllocator() const {
return resource_allocator_;
}

// |Context|
std::shared_ptr<ShaderLibrary> ContextGLES::GetShaderLibrary() const {
return shader_library_;
}

// |Context|
std::shared_ptr<SamplerLibrary> ContextGLES::GetSamplerLibrary() const {
return sampler_library_;
}

// |Context|
std::shared_ptr<PipelineLibrary> ContextGLES::GetPipelineLibrary() const {
return pipeline_library_;
}

// |Context|
std::shared_ptr<CommandBuffer> ContextGLES::CreateCommandBuffer() const {
return std::shared_ptr<CommandBufferGLES>(
new CommandBufferGLES(weak_from_this(), reactor_));
}

// |Context|
std::shared_ptr<WorkQueue> ContextGLES::GetWorkQueue() const {
return work_queue_;
}

// |Context|
bool ContextGLES::HasThreadingRestrictions() const {
return true;
Expand Down
4 changes: 4 additions & 0 deletions impeller/renderer/backend/gles/context_gles.h
Original file line number Diff line number Diff line change
Expand Up @@ -38,6 +38,7 @@ class ContextGLES final : public Context,
std::shared_ptr<ShaderLibraryGLES> shader_library_;
std::shared_ptr<PipelineLibraryGLES> pipeline_library_;
std::shared_ptr<SamplerLibraryGLES> sampler_library_;
std::shared_ptr<WorkQueue> work_queue_;
std::shared_ptr<AllocatorGLES> resource_allocator_;
bool is_valid_ = false;

Expand All @@ -62,6 +63,9 @@ class ContextGLES final : public Context,
// |Context|
std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;

// |Context|
std::shared_ptr<WorkQueue> GetWorkQueue() const override;

// |Context|
bool HasThreadingRestrictions() const override;

Expand Down
4 changes: 4 additions & 0 deletions impeller/renderer/backend/metal/context_mtl.h
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,7 @@ class ContextMTL final : public Context,
std::shared_ptr<PipelineLibraryMTL> pipeline_library_;
std::shared_ptr<SamplerLibrary> sampler_library_;
std::shared_ptr<AllocatorMTL> resource_allocator_;
std::shared_ptr<WorkQueue> work_queue_;
bool is_valid_ = false;

ContextMTL(id<MTLDevice> device, NSArray<id<MTLLibrary>>* shader_libraries);
Expand All @@ -64,6 +65,9 @@ class ContextMTL final : public Context,
// |Context|
std::shared_ptr<CommandBuffer> CreateCommandBuffer() const override;

// |Context|
std::shared_ptr<WorkQueue> GetWorkQueue() const override;

std::shared_ptr<CommandBuffer> CreateCommandBufferInQueue(
id<MTLCommandQueue> queue) const;

Expand Down
Loading