Accumulate a batch of Skia objects that will be destructed later on the IO thread (flutter/engine#3888)
See https://github.com/dart-lang/sdk/issues/29971
This commit is contained in:
@@ -42,6 +42,7 @@ source_set("ui") {
|
||||
"painting/rrect.h",
|
||||
"painting/shader.cc",
|
||||
"painting/shader.h",
|
||||
"painting/utils.cc",
|
||||
"painting/utils.h",
|
||||
"painting/vertices.cc",
|
||||
"painting/vertices.h",
|
||||
|
||||
48
engine/src/flutter/lib/ui/painting/utils.cc
Normal file
48
engine/src/flutter/lib/ui/painting/utils.cc
Normal file
@@ -0,0 +1,48 @@
|
||||
// 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/common/threads.h"
|
||||
#include "flutter/lib/ui/painting/utils.h"
|
||||
|
||||
namespace blink {
|
||||
|
||||
namespace {
|
||||
|
||||
constexpr ftl::TimeDelta kDrainDelay = ftl::TimeDelta::FromMilliseconds(250);
|
||||
|
||||
} // anonymous namespace
|
||||
|
||||
SkiaUnrefQueue::SkiaUnrefQueue()
|
||||
: drain_pending_(false) {}
|
||||
|
||||
SkiaUnrefQueue SkiaUnrefQueue::instance_;
|
||||
|
||||
SkiaUnrefQueue& SkiaUnrefQueue::Get() {
|
||||
return instance_;
|
||||
}
|
||||
|
||||
void SkiaUnrefQueue::Unref(SkRefCnt* object) {
|
||||
ftl::MutexLocker lock(&mutex_);
|
||||
objects_.push_back(object);
|
||||
if (!drain_pending_) {
|
||||
drain_pending_ = true;
|
||||
Threads::IO()->PostDelayedTask([this] { Drain(); },
|
||||
kDrainDelay);
|
||||
}
|
||||
}
|
||||
|
||||
void SkiaUnrefQueue::Drain() {
|
||||
std::deque<SkRefCnt*> skia_objects;
|
||||
{
|
||||
ftl::MutexLocker lock(&mutex_);
|
||||
objects_.swap(skia_objects);
|
||||
drain_pending_ = false;
|
||||
}
|
||||
|
||||
for (SkRefCnt* skia_object : skia_objects) {
|
||||
skia_object->unref();
|
||||
}
|
||||
}
|
||||
|
||||
} // namespace blink
|
||||
@@ -2,15 +2,35 @@
|
||||
// Use of this source code is governed by a BSD-style license that can be
|
||||
// found in the LICENSE file.
|
||||
|
||||
#include "flutter/common/threads.h"
|
||||
#include "lib/ftl/synchronization/mutex.h"
|
||||
#include "third_party/skia/include/core/SkRefCnt.h"
|
||||
|
||||
#include <queue>
|
||||
|
||||
namespace blink {
|
||||
|
||||
// A queue that holds Skia objects that must be destructed on the IO thread.
|
||||
class SkiaUnrefQueue {
|
||||
public:
|
||||
static SkiaUnrefQueue& Get();
|
||||
|
||||
void Unref(SkRefCnt* object);
|
||||
|
||||
private:
|
||||
SkiaUnrefQueue();
|
||||
void Drain();
|
||||
|
||||
static SkiaUnrefQueue instance_;
|
||||
|
||||
ftl::Mutex mutex_;
|
||||
std::deque<SkRefCnt*> objects_ FTL_GUARDED_BY(mutex_);
|
||||
bool drain_pending_ FTL_GUARDED_BY(mutex_);
|
||||
};
|
||||
|
||||
template <typename T> void SkiaUnrefOnIOThread(sk_sp<T>* sp) {
|
||||
T* object = sp->release();
|
||||
if (object) {
|
||||
Threads::IO()->PostTask([object]() { object->unref(); });
|
||||
SkiaUnrefQueue::Get().Unref(object);
|
||||
}
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user