1 // Copyright 2013 The Flutter Authors. All rights reserved. 2 // Use of this source code is governed by a BSD-style license that can be 3 // found in the LICENSE file. 4 5 #include "flutter/flow/skia_gpu_object.h" 6 7 #include "flutter/fml/message_loop.h" 8 9 namespace flutter { 10 SkiaUnrefQueue(fml::RefPtr<fml::TaskRunner> task_runner,fml::TimeDelta delay)11SkiaUnrefQueue::SkiaUnrefQueue(fml::RefPtr<fml::TaskRunner> task_runner, 12 fml::TimeDelta delay) 13 : task_runner_(std::move(task_runner)), 14 drain_delay_(delay), 15 drain_pending_(false) {} 16 ~SkiaUnrefQueue()17SkiaUnrefQueue::~SkiaUnrefQueue() { 18 FML_DCHECK(objects_.empty()); 19 } 20 Unref(SkRefCnt * object)21void SkiaUnrefQueue::Unref(SkRefCnt* object) { 22 std::scoped_lock lock(mutex_); 23 objects_.push_back(object); 24 if (invalid_) { 25 FML_LOG(ERROR) << "Unref called after queue invalid!"; 26 return; 27 } 28 if (!drain_pending_) { 29 drain_pending_ = true; 30 task_runner_->PostDelayedTask( 31 [strong = fml::Ref(this)]() { strong->Drain(); }, drain_delay_); 32 } 33 } 34 Drain(bool finish)35void SkiaUnrefQueue::Drain(bool finish) { 36 std::deque<SkRefCnt*> skia_objects; 37 { 38 std::scoped_lock lock(mutex_); 39 objects_.swap(skia_objects); 40 drain_pending_ = false; 41 if (finish) { 42 invalid_ = finish; 43 } 44 } 45 46 for (SkRefCnt* skia_object : skia_objects) { 47 skia_object->unref(); 48 } 49 } 50 51 } // namespace flutter 52