Skip to content

Commit

Permalink
Merge subset of pull request #697 from emutavchi/wpe-2.22-wip into wp…
Browse files Browse the repository at this point in the history
…e-2.22
  • Loading branch information
eocanha committed Feb 25, 2021
2 parents 03dc152 + 2629937 commit 941e508
Show file tree
Hide file tree
Showing 38 changed files with 310 additions and 79 deletions.
6 changes: 3 additions & 3 deletions Source/JavaScriptCore/inspector/AsyncStackTrace.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -167,18 +167,18 @@ void AsyncStackTrace::truncate(size_t maxDepth)

// The subtree being truncated must be removed from it's parent before
// updating its parent pointer chain.
auto* sourceNode = lastUnlockedAncestor->m_parent.get();
RefPtr<AsyncStackTrace> sourceNode = lastUnlockedAncestor->m_parent;
lastUnlockedAncestor->remove();

while (sourceNode) {
previousNode->m_parent = AsyncStackTrace::create(sourceNode->m_callStack.copyRef(), true, nullptr);
previousNode->m_parent->m_childCount = 1;
previousNode = previousNode->m_parent.get();

if (sourceNode == newStackTraceRoot)
if (sourceNode.get() == newStackTraceRoot)
break;

sourceNode = sourceNode->m_parent.get();
sourceNode = sourceNode->m_parent;
}

previousNode->m_truncated = true;
Expand Down
5 changes: 4 additions & 1 deletion Source/WTF/wtf/MemoryPressureHandler.h
Original file line number Diff line number Diff line change
Expand Up @@ -70,7 +70,7 @@ class MemoryPressureHandler {
WTF_EXPORT_PRIVATE void setShouldUsePeriodicMemoryMonitor(bool);

#if OS(LINUX)
WTF_EXPORT_PRIVATE void triggerMemoryPressureEvent(bool isCritical);
WTF_EXPORT_PRIVATE void triggerMemoryPressureEvent(bool isCritical, bool isSynchronous);
#endif

void setMemoryKillCallback(WTF::Function<void()>&& function) { m_memoryKillCallback = WTFMove(function); }
Expand Down Expand Up @@ -198,6 +198,8 @@ class MemoryPressureHandler {
RunLoop::Timer<MemoryPressureHandler> m_holdOffTimer;
void holdOffTimerFired();

struct MemoryUsagePollerThreadContext;

class MemoryUsagePoller {
WTF_MAKE_NONCOPYABLE(MemoryUsagePoller); WTF_MAKE_FAST_ALLOCATED;
public:
Expand All @@ -206,6 +208,7 @@ class MemoryPressureHandler {

private:
RefPtr<Thread> m_thread;
RefPtr<MemoryUsagePollerThreadContext> m_context;
};

std::unique_ptr<MemoryUsagePoller> m_memoryUsagePoller;
Expand Down
44 changes: 35 additions & 9 deletions Source/WTF/wtf/linux/MemoryPressureHandlerLinux.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -219,20 +219,44 @@ static bool initializeProcessGPUMemoryLimits(size_t &criticalLimit, size_t &nonC
return true;
}

struct MemoryPressureHandler::MemoryUsagePollerThreadContext
: public ThreadSafeRefCounted<MemoryPressureHandler::MemoryUsagePollerThreadContext>
{
void stop()
{
LockHolder locker(m_lock);
m_shouldStop = true;
m_condition.notifyAll();
}

// returns false when should stop polling
bool sleep(const Seconds timeout)
{
LockHolder locker(m_lock);
return !m_condition.waitFor(m_lock, timeout, [this]() { return m_shouldStop; });
}

Lock m_lock;
bool m_shouldStop { false };
Condition m_condition;
};

MemoryPressureHandler::MemoryUsagePoller::MemoryUsagePoller()
{
m_thread = Thread::create("WTF: MemoryPressureHandler", [this] {
m_context = adoptRef(new MemoryPressureHandler::MemoryUsagePollerThreadContext());
m_thread = Thread::create("WTF: MemoryPressureHandler", [this, context = m_context] {
do {
bool underMemoryPressure = false;
bool critical = false;
bool synchronous = false;
size_t value = 0;

if (s_pollMaximumProcessMemoryCriticalLimit) {
if (readToken(s_processStatus, "VmRSS:", KB, value)) {
if (value > s_pollMaximumProcessMemoryNonCriticalLimit) {
underMemoryPressure = true;
critical = value > s_pollMaximumProcessMemoryCriticalLimit;
synchronous = value > s_pollMaximumProcessMemoryCriticalLimit * 1.05;
}
}
}
Expand All @@ -247,40 +271,42 @@ MemoryPressureHandler::MemoryUsagePoller::MemoryUsagePoller()
}

if (underMemoryPressure) {
callOnMainThread([critical] {
MemoryPressureHandler::singleton().triggerMemoryPressureEvent(critical);
callOnMainThread([critical, synchronous] {
MemoryPressureHandler::singleton().triggerMemoryPressureEvent(critical, synchronous);
});
return;
}

sleep(s_memoryUsagePollerInterval);
if (!context->sleep(s_memoryUsagePollerInterval))
return;
} while (true);
});
}

MemoryPressureHandler::MemoryUsagePoller::~MemoryUsagePoller()
{
m_context->stop();
if (m_thread)
m_thread->detach();
}



void MemoryPressureHandler::triggerMemoryPressureEvent(bool isCritical)
void MemoryPressureHandler::triggerMemoryPressureEvent(bool isCritical, bool isSynchronous)
{
if (!m_installed)
return;

if (ReliefLogger::loggingEnabled())
LOG(MemoryPressure, "Got memory pressure notification (%s)", isCritical ? "critical" : "non-critical");
LOG(MemoryPressure, "Got memory pressure notification (%s, %s) ", isCritical ? "critical" : "non-critical", isSynchronous ? "synchronous" : "non-synchronous");

setUnderMemoryPressure(true);

if (isMainThread())
respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No, isCritical ? Synchronous::Yes : Synchronous::No);
respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No, isSynchronous ? Synchronous::Yes : Synchronous::No);
else
RunLoop::main().dispatch([this, isCritical] {
respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No, isCritical ? Synchronous::Yes : Synchronous::No);
RunLoop::main().dispatch([this, isCritical, isSynchronous] {
respondToMemoryPressure(isCritical ? Critical::Yes : Critical::No, isSynchronous ? Synchronous::Yes : Synchronous::No);
});

if (ReliefLogger::loggingEnabled() && isUnderMemoryPressure())
Expand Down
3 changes: 1 addition & 2 deletions Source/WebCore/Modules/encryptedmedia/MediaKeySession.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -753,8 +753,7 @@ String MediaKeySession::mediaKeysStorageDirectory() const

bool MediaKeySession::hasPendingActivity() const
{
notImplemented();
return false;
return m_eventQueue.hasPendingEvents() || m_taskQueue.hasPendingTasks();
}

const char* MediaKeySession::activeDOMObjectName() const
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/Modules/encryptedmedia/MediaKeySession.h
Original file line number Diff line number Diff line change
Expand Up @@ -77,6 +77,8 @@ class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTa

const Vector<std::pair<Ref<SharedBuffer>, MediaKeyStatus>>& statuses() const { return m_statuses; }

bool hasPendingActivity() const final;

private:
MediaKeySession(ScriptExecutionContext&, WeakPtr<MediaKeys>&&, MediaKeySessionType, bool useDistinctiveIdentifier, Ref<CDM>&&, Ref<CDMInstance>&&);
void enqueueMessage(MediaKeyMessageType, const SharedBuffer&);
Expand All @@ -95,7 +97,6 @@ class MediaKeySession final : public RefCounted<MediaKeySession>, public EventTa
void derefEventTarget() override { deref(); }

// ActiveDOMObject
bool hasPendingActivity() const override;
const char* activeDOMObjectName() const override;
bool canSuspendForDocumentSuspension() const override;
void stop() override;
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/Modules/encryptedmedia/MediaKeySession.idl
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@
*/

[
ActiveDOMObject,
Conditional=ENCRYPTED_MEDIA,
EnabledAtRuntime=EncryptedMediaAPI
] interface MediaKeySession : EventTarget {
Expand Down
3 changes: 2 additions & 1 deletion Source/WebCore/Modules/mediasource/MediaSource.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -263,7 +263,8 @@ void MediaSource::completeSeek()
// 4. Resume the seek algorithm at the "Await a stable state" step.
m_private->seekCompleted();

monitorSourceBuffers();
if (m_pendingSeekTime.isInvalid())
monitorSourceBuffers();
}

Ref<TimeRanges> MediaSource::seekable()
Expand Down
46 changes: 30 additions & 16 deletions Source/WebCore/Modules/mediasource/SourceBuffer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,19 @@ static inline bool mediaSourceLogEnabled()
#endif
}

static inline MediaTime roundTowardsTimeScaleWithRoundingMargin(const MediaTime& time, uint32_t timeScale, const MediaTime& roundingMargin)
{
while (true) {
if (time.timeScale() == timeScale)
return time;
MediaTime roundedTime = time.toTimeScale(timeScale);
if (abs(roundedTime - time) < roundingMargin || timeScale >= MediaTime::MaximumTimeScale)
return roundedTime;
if (!WTF::safeMultiply(timeScale, 2, timeScale) || timeScale > MediaTime::MaximumTimeScale)
timeScale = MediaTime::MaximumTimeScale;
}
};

static const double ExponentialMovingAverageCoefficient = 0.1;

struct SourceBuffer::TrackBuffer {
Expand Down Expand Up @@ -763,7 +776,7 @@ static PlatformTimeRanges removeSamplesFromTrackBuffer(const DecodeOrderSampleMa
additionalErasedRanges.add(previousSample.presentationTime() + previousSample.duration(), erasedStart);
}

auto endIterator = trackBuffer.samples.presentationOrder().findSampleStartingOnOrAfterPresentationTime(erasedEnd);
auto endIterator = trackBuffer.samples.presentationOrder().findSampleContainingOrAfterPresentationTime(erasedEnd);
if (endIterator == trackBuffer.samples.presentationOrder().end())
additionalErasedRanges.add(erasedEnd, MediaTime::positiveInfiniteTime());
else {
Expand Down Expand Up @@ -812,7 +825,9 @@ void SourceBuffer::removeCodedFrames(const MediaTime& start, const MediaTime& en
RefPtr<MediaSample> sample = sampleIterator->second;
if (!sample->isDivisable())
return;
std::pair<RefPtr<MediaSample>, RefPtr<MediaSample>> replacementSamples = sample->divide(time);
MediaTime microsecond(1, 1000000);
MediaTime roundedTime = roundTowardsTimeScaleWithRoundingMargin(time, sample->presentationTime().timeScale(), microsecond);
std::pair<RefPtr<MediaSample>, RefPtr<MediaSample>> replacementSamples = sample->divide(roundedTime);
if (!replacementSamples.first || !replacementSamples.second)
return;
LOG(MediaSource, "SourceBuffer::removeCodedFrames(%p) - splitting sample (%s) into\n\t(%s)\n\t(%s)", this,
Expand Down Expand Up @@ -962,13 +977,17 @@ void SourceBuffer::evictCodedFrames(size_t newDataSize)
MediaTime maximumRangeEnd = currentTime - thirtySeconds;

#if defined(METROLOGICAL)
MediaTime microSecond = MediaTime(1, 1000000);
for (auto& trackBuffer : m_trackBufferMap.values()) {
auto t = std::max(MediaTime::zeroTime(), currentTime - MediaTime(2, 1));
auto prevSync =
trackBuffer.samples.decodeOrder().findSyncSamplePriorToPresentationTime(currentTime);
trackBuffer.samples.decodeOrder().findSyncSamplePriorToPresentationTime(t);
if (prevSync != trackBuffer.samples.decodeOrder().rend()) {
// Don't include the sync frame in the range, just finish right before it.
maximumRangeEnd = prevSync->second->presentationTime() - microSecond;
prevSync++;
}
if (prevSync != trackBuffer.samples.decodeOrder().rend()) {
maximumRangeEnd = prevSync->second->presentationTime();
break;
}
}
#endif
Expand All @@ -983,6 +1002,12 @@ void SourceBuffer::evictCodedFrames(size_t newDataSize)

if (MediaTime::zeroTime() != rangeStart && rangeStart < maximumRangeEnd) {
removeCodedFrames(MediaTime::zeroTime(), rangeStart, true);
// Check if removed enough already
if (extraMemoryCost() + newDataSize < maximumBufferSize) {
LOG(MediaSource, "SourceBuffer::evictCodedFrames(%p) - the buffer is not full anymore.", this);
m_bufferFull = false;
return;
}
}

while (rangeStart < maximumRangeEnd) {
Expand Down Expand Up @@ -1597,17 +1622,6 @@ void SourceBuffer::sourceBufferPrivateDidReceiveSample(MediaSample& sample)

MediaTime microsecond(1, 1000000);

auto roundTowardsTimeScaleWithRoundingMargin = [] (const MediaTime& time, uint32_t timeScale, const MediaTime& roundingMargin) {
while (true) {
MediaTime roundedTime = time.toTimeScale(timeScale);
if (abs(roundedTime - time) < roundingMargin || timeScale >= MediaTime::MaximumTimeScale)
return roundedTime;

if (!WTF::safeMultiply(timeScale, 2, timeScale) || timeScale > MediaTime::MaximumTimeScale)
timeScale = MediaTime::MaximumTimeScale;
}
};

// 1.4 If timestampOffset is not 0, then run the following steps:
if (m_timestampOffset) {
if (!trackBuffer.roundedTimestampOffset.isValid() || presentationTimestamp.timeScale() != trackBuffer.lastFrameTimescale) {
Expand Down
10 changes: 7 additions & 3 deletions Source/WebCore/html/HTMLMediaElement.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -2203,7 +2203,9 @@ void HTMLMediaElement::noneSupported()

// 6.1 - Set the error attribute to a new MediaError object whose code attribute is set to
// MEDIA_ERR_SRC_NOT_SUPPORTED.
m_error = MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);
m_error = m_player
? MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED, m_player->errorMessage())
: MediaError::create(MediaError::MEDIA_ERR_SRC_NOT_SUPPORTED);

// 6.2 - Forget the media element's media-resource-specific text tracks.
forgetResourceSpecificTracks();
Expand Down Expand Up @@ -2236,12 +2238,14 @@ void HTMLMediaElement::mediaLoadingFailedFatally(MediaPlayer::NetworkState error
stopPeriodicTimers();
m_loadState = WaitingForSource;

const auto getErrorMessage = [&] () { return m_player ? m_player->errorMessage() : emptyString(); };

// 2 - Set the error attribute to a new MediaError object whose code attribute is
// set to MEDIA_ERR_NETWORK/MEDIA_ERR_DECODE.
if (error == MediaPlayer::NetworkError)
m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK);
m_error = MediaError::create(MediaError::MEDIA_ERR_NETWORK, getErrorMessage());
else if (error == MediaPlayer::DecodeError)
m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE);
m_error = MediaError::create(MediaError::MEDIA_ERR_DECODE, getErrorMessage());
else
ASSERT_NOT_REACHED();

Expand Down
2 changes: 1 addition & 1 deletion Source/WebCore/html/HTMLMediaElement.h
Original file line number Diff line number Diff line change
Expand Up @@ -589,7 +589,7 @@ class HTMLMediaElement
bool isMediaElement() const final { return true; }

#if ENABLE(VIDEO_TRACK)
bool ignoreTrackDisplayUpdateRequests() const { return m_ignoreTrackDisplayUpdate > 0 || !m_textTracks || !m_cueTree.size(); }
bool ignoreTrackDisplayUpdateRequests() const { return m_ignoreTrackDisplayUpdate > 0 || !m_textTracks || m_cueTree.isEmpty(); }
void beginIgnoringTrackDisplayUpdateRequests();
void endIgnoringTrackDisplayUpdateRequests();
#endif
Expand Down
4 changes: 4 additions & 0 deletions Source/WebCore/html/MediaError.h
Original file line number Diff line number Diff line change
Expand Up @@ -44,13 +44,17 @@ class MediaError : public RefCounted<MediaError> {
};

static Ref<MediaError> create(Code code) { return adoptRef(*new MediaError(code)); }
static Ref<MediaError> create(Code code, String message) { return adoptRef(*new MediaError(code, message)); }

Code code() const { return m_code; }
String message() const { return m_message; }

private:
MediaError(Code code) : m_code(code) { }
MediaError(Code code, String message) : m_code(code), m_message(message) { }

Code m_code;
String m_message;
};

} // namespace WebCore
Expand Down
1 change: 1 addition & 0 deletions Source/WebCore/html/MediaError.idl
Original file line number Diff line number Diff line change
Expand Up @@ -33,4 +33,5 @@
const unsigned short MEDIA_ERR_SRC_NOT_SUPPORTED = 4;
[Conditional=LEGACY_ENCRYPTED_MEDIA] const unsigned short MEDIA_ERR_ENCRYPTED = 5;
readonly attribute unsigned short code;
readonly attribute DOMString message;
};
7 changes: 7 additions & 0 deletions Source/WebCore/loader/cache/CachedImage.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -674,4 +674,11 @@ CachedResource::RevalidationDecision CachedImage::makeRevalidationDecision(Cache
return CachedResource::makeRevalidationDecision(cachePolicy);
}

bool CachedImage::mayTryReplaceEncodedData() const {
if (hasImage() && is<BitmapImage>(m_image.get())) {
return !downcast<BitmapImage>(m_image.get())->canUseAsyncDecodingForLargeImages();
}
return true;
}

} // namespace WebCore
2 changes: 1 addition & 1 deletion Source/WebCore/loader/cache/CachedImage.h
Original file line number Diff line number Diff line change
Expand Up @@ -108,7 +108,7 @@ class CachedImage final : public CachedResource {
void checkShouldPaintBrokenImage();

void switchClientsToRevalidatedResource() final;
bool mayTryReplaceEncodedData() const final { return true; }
bool mayTryReplaceEncodedData() const final;

void didAddClient(CachedResourceClient&) final;
void didRemoveClient(CachedResourceClient&) final;
Expand Down
5 changes: 5 additions & 0 deletions Source/WebCore/platform/PODRedBlackTree.h
Original file line number Diff line number Diff line change
Expand Up @@ -154,6 +154,11 @@ class PODRedBlackTree {
return counter.count();
}

bool isEmpty() const
{
return m_root == nullptr;
}

// See the class documentation for an explanation of this property.
void setNeedsFullOrderingComparisons(bool needsFullOrderingComparisons)
{
Expand Down
7 changes: 6 additions & 1 deletion Source/WebCore/platform/graphics/MediaPlayer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1174,7 +1174,7 @@ void MediaPlayer::networkStateChanged()
// let the next engine try.
if (m_private->networkState() >= FormatError && m_private->readyState() < HaveMetadata) {
client().mediaPlayerEngineFailedToLoad();
if (installedMediaEngines().size() > 1 && (m_contentType.isEmpty() || nextBestMediaEngine(m_currentMediaEngine))) {
if (installedMediaEngines().size() > 1 && !m_contentType.isEmpty() && nextBestMediaEngine(m_currentMediaEngine)) {
m_reloadTimer.startOneShot(0_s);
return;
}
Expand Down Expand Up @@ -1617,6 +1617,11 @@ String convertEnumerationToString(MediaPlayerEnums::Preload enumerationValue)
return values[static_cast<size_t>(enumerationValue)];
}

String MediaPlayer::errorMessage() const
{
return m_private->errorMessage();
}

}

#endif
Loading

0 comments on commit 941e508

Please sign in to comment.