Skip to content

Commit

Permalink
Accumulate a batch of Skia objects that will be destructed later on t…
Browse files Browse the repository at this point in the history
…he IO thread (flutter#3888)

See dart-lang/sdk#29971
  • Loading branch information
jason-simmons authored Jul 18, 2017
1 parent bd22943 commit dd2fb3c
Show file tree
Hide file tree
Showing 4 changed files with 72 additions and 2 deletions.
1 change: 1 addition & 0 deletions lib/ui/BUILD.gn
Original file line number Diff line number Diff line change
Expand Up @@ -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",
Expand Down
48 changes: 48 additions & 0 deletions lib/ui/painting/utils.cc
Original file line number Diff line number Diff line change
@@ -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
24 changes: 22 additions & 2 deletions lib/ui/painting/utils.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
1 change: 1 addition & 0 deletions travis/licenses_golden/licenses_flutter
Original file line number Diff line number Diff line change
Expand Up @@ -1424,6 +1424,7 @@ FILE: ../../../flutter/fml/thread_local_unittests.cc
FILE: ../../../flutter/fml/thread_unittests.cc
FILE: ../../../flutter/fml/trace_event.cc
FILE: ../../../flutter/fml/trace_event.h
FILE: ../../../flutter/lib/ui/painting/utils.cc
FILE: ../../../flutter/lib/ui/painting/vertices.cc
FILE: ../../../flutter/lib/ui/painting/vertices.h
FILE: ../../../flutter/shell/gpu/gpu_surface_software.cc
Expand Down

0 comments on commit dd2fb3c

Please sign in to comment.