1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130
|
// Copyright 2022 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "third_party/blink/renderer/modules/webcodecs/codec_pressure_manager.h"
#include "base/task/sequenced_task_runner.h"
#include "build/build_config.h"
#include "third_party/blink/renderer/modules/webcodecs/codec_pressure_gauge.h"
#include "third_party/blink/renderer/platform/heap/cross_thread_persistent.h"
#include "third_party/blink/renderer/platform/scheduler/public/post_cross_thread_task.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_copier_base.h"
#include "third_party/blink/renderer/platform/wtf/cross_thread_functional.h"
#include "third_party/blink/renderer/platform/wtf/functional.h"
namespace blink {
CodecPressureManager::CodecPressureManager(
ReclaimableCodec::CodecType codec_type,
scoped_refptr<base::SequencedTaskRunner> task_runner)
: codec_type_(codec_type) {
auto pressure_threshold_changed_cb =
[](CrossThreadWeakPersistent<CodecPressureManager> self,
scoped_refptr<base::SequencedTaskRunner> task_runner,
bool global_pressure_exceeded) {
// Accessing |self| is not thread safe. Even if it is thread unsafe,
// checking for |!self| can definitively tell us if the object has
// already been GC'ed, so we can exit early. Otherwise, we always post
// to |task_runner|, where it will be safe to use |self|, since it's
// in the same sequence that created |self|.
if (!self)
return;
// Always post this change, to guarantee ordering if this callback
// is run from different threads.
DCHECK(task_runner);
PostCrossThreadTask(
*task_runner, FROM_HERE,
CrossThreadBindOnce(
&CodecPressureManager::OnGlobalPressureThresholdChanged, self,
global_pressure_exceeded));
};
CodecPressureGauge::RegistrationResult result =
GetCodecPressureGauge().RegisterPressureCallback(
ConvertToBaseRepeatingCallback(CrossThreadBindRepeating(
pressure_threshold_changed_cb,
WrapCrossThreadWeakPersistent(this), task_runner)));
pressure_callback_id_ = result.first;
global_pressure_exceeded_ = result.second;
}
CodecPressureGauge& CodecPressureManager::GetCodecPressureGauge() {
return CodecPressureGauge::GetInstance(codec_type_);
}
void CodecPressureManager::AddCodec(ReclaimableCodec* codec) {
DCHECK(manager_registered_);
DCHECK(codec->is_applying_codec_pressure());
DCHECK(!codecs_with_pressure_.Contains(codec));
codecs_with_pressure_.insert(codec);
++local_codec_pressure_;
GetCodecPressureGauge().Increment();
codec->SetGlobalPressureExceededFlag(global_pressure_exceeded_);
}
void CodecPressureManager::RemoveCodec(ReclaimableCodec* codec) {
DCHECK(manager_registered_);
DCHECK(codecs_with_pressure_.Contains(codec));
codecs_with_pressure_.erase(codec);
DCHECK(local_codec_pressure_);
--local_codec_pressure_;
GetCodecPressureGauge().Decrement();
// |codec| is responsible for clearing its own global pressure exceeded flag.
}
void CodecPressureManager::OnCodecDisposed(ReclaimableCodec* codec) {
DCHECK(codec->is_applying_codec_pressure());
if (!manager_registered_) {
// |this|'s pre-finalizer (UnregisterManager()) could have been called
// before leftover ReclaimableCodec's pre-finalizers were called.
// This shouldn't happen often, but it might if |this| and codecs are
// prefinalized in the same GC run.
DCHECK_EQ(local_codec_pressure_, 0u);
return;
}
// The GC should have removed |codec| from |codecs_with_pressure_|.
DCHECK(!codecs_with_pressure_.Contains(codec));
DCHECK(local_codec_pressure_);
--local_codec_pressure_;
GetCodecPressureGauge().Decrement();
}
void CodecPressureManager::OnGlobalPressureThresholdChanged(
bool pressure_threshold_exceeded) {
DCHECK_NE(global_pressure_exceeded_, pressure_threshold_exceeded);
global_pressure_exceeded_ = pressure_threshold_exceeded;
for (auto codec : codecs_with_pressure_)
codec->SetGlobalPressureExceededFlag(global_pressure_exceeded_);
}
void CodecPressureManager::UnregisterManager() {
if (!manager_registered_)
return;
GetCodecPressureGauge().UnregisterPressureCallback(pressure_callback_id_,
local_codec_pressure_);
codecs_with_pressure_.clear();
local_codec_pressure_ = 0u;
manager_registered_ = false;
}
void CodecPressureManager::Trace(Visitor* visitor) const {
visitor->Trace(codecs_with_pressure_);
}
} // namespace blink
|