Title: [276223] trunk/Source/WebKit
Revision
276223
Author
[email protected]
Date
2021-04-18 00:10:05 -0700 (Sun, 18 Apr 2021)

Log Message

Update LibWebRTCCodecsProxy to use a Lock
https://bugs.webkit.org/show_bug.cgi?id=224728

Reviewed by Darin Adler.

Update LibWebRTCCodecsProxy to use a Lock, instead of a std::atomic<bool> that
has to be kept up to date. I think this simplifies the code a bit. Adding / Removing
encoder / decoder is not very hot code as far as I know and there will very rarely
be contention since allowsExitUnderMemoryPressure() is only called on memory pressure.

m_encoder / m_decoder are still always modified from the background thread. However, we
now check from the main thread if they are empty by locking.

* GPUProcess/webrtc/LibWebRTCCodecsProxy.h:
* GPUProcess/webrtc/LibWebRTCCodecsProxy.mm:
(WebKit::LibWebRTCCodecsProxy::close):
(WebKit::LibWebRTCCodecsProxy::createH264Decoder):
(WebKit::LibWebRTCCodecsProxy::createH265Decoder):
(WebKit::LibWebRTCCodecsProxy::createVP9Decoder):
(WebKit::LibWebRTCCodecsProxy::releaseDecoder):
(WebKit::LibWebRTCCodecsProxy::decodeFrame):
(WebKit::LibWebRTCCodecsProxy::setFrameSize):
(WebKit::LibWebRTCCodecsProxy::createEncoder):
(WebKit::LibWebRTCCodecsProxy::releaseEncoder):
(WebKit::LibWebRTCCodecsProxy::initializeEncoder):
(WebKit::LibWebRTCCodecsProxy::encodeFrame):
(WebKit::LibWebRTCCodecsProxy::setEncodeRates):
(WebKit::LibWebRTCCodecsProxy::allowsExitUnderMemoryPressure const):
(WebKit::LibWebRTCCodecsProxy::updateHasEncodersOrDecoders): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebKit/ChangeLog (276222 => 276223)


--- trunk/Source/WebKit/ChangeLog	2021-04-18 05:23:02 UTC (rev 276222)
+++ trunk/Source/WebKit/ChangeLog	2021-04-18 07:10:05 UTC (rev 276223)
@@ -1,3 +1,35 @@
+2021-04-18  Chris Dumez  <[email protected]>
+
+        Update LibWebRTCCodecsProxy to use a Lock
+        https://bugs.webkit.org/show_bug.cgi?id=224728
+
+        Reviewed by Darin Adler.
+
+        Update LibWebRTCCodecsProxy to use a Lock, instead of a std::atomic<bool> that
+        has to be kept up to date. I think this simplifies the code a bit. Adding / Removing
+        encoder / decoder is not very hot code as far as I know and there will very rarely
+        be contention since allowsExitUnderMemoryPressure() is only called on memory pressure.
+
+        m_encoder / m_decoder are still always modified from the background thread. However, we
+        now check from the main thread if they are empty by locking.
+
+        * GPUProcess/webrtc/LibWebRTCCodecsProxy.h:
+        * GPUProcess/webrtc/LibWebRTCCodecsProxy.mm:
+        (WebKit::LibWebRTCCodecsProxy::close):
+        (WebKit::LibWebRTCCodecsProxy::createH264Decoder):
+        (WebKit::LibWebRTCCodecsProxy::createH265Decoder):
+        (WebKit::LibWebRTCCodecsProxy::createVP9Decoder):
+        (WebKit::LibWebRTCCodecsProxy::releaseDecoder):
+        (WebKit::LibWebRTCCodecsProxy::decodeFrame):
+        (WebKit::LibWebRTCCodecsProxy::setFrameSize):
+        (WebKit::LibWebRTCCodecsProxy::createEncoder):
+        (WebKit::LibWebRTCCodecsProxy::releaseEncoder):
+        (WebKit::LibWebRTCCodecsProxy::initializeEncoder):
+        (WebKit::LibWebRTCCodecsProxy::encodeFrame):
+        (WebKit::LibWebRTCCodecsProxy::setEncodeRates):
+        (WebKit::LibWebRTCCodecsProxy::allowsExitUnderMemoryPressure const):
+        (WebKit::LibWebRTCCodecsProxy::updateHasEncodersOrDecoders): Deleted.
+
 2021-04-17  Chris Dumez  <[email protected]>
 
         GPUConnectionToWebProcess::allowsExitUnderMemoryPressure() should check if libWebRTCCodecsProxy is used

Modified: trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.h (276222 => 276223)


--- trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.h	2021-04-18 05:23:02 UTC (rev 276222)
+++ trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.h	2021-04-18 07:10:05 UTC (rev 276223)
@@ -82,14 +82,13 @@
     void encodeFrame(RTCEncoderIdentifier, WebCore::RemoteVideoSample&&, uint32_t timeStamp, bool shouldEncodeAsKeyFrame);
     void setEncodeRates(RTCEncoderIdentifier, uint32_t bitRate, uint32_t frameRate);
 
-    void updateHasEncodersOrDecoders();
-
     CFDictionaryRef ioSurfacePixelBufferCreationOptions(IOSurfaceRef);
 
     GPUConnectionToWebProcess& m_gpuConnectionToWebProcess;
+
+    mutable Lock m_lock;
     HashMap<RTCDecoderIdentifier, webrtc::LocalDecoder> m_decoders;
     HashMap<RTCEncoderIdentifier, webrtc::LocalEncoder> m_encoders;
-    std::atomic<bool> m_hasEncodersOrDecoders;
 
     Ref<WorkQueue> m_queue;
     std::unique_ptr<WebCore::ImageTransferSessionVT> m_imageTransferSession;

Modified: trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.mm (276222 => 276223)


--- trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.mm	2021-04-18 05:23:02 UTC (rev 276222)
+++ trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.mm	2021-04-18 07:10:05 UTC (rev 276223)
@@ -62,6 +62,7 @@
     m_gpuConnectionToWebProcess.connection().removeThreadMessageReceiver(Messages::LibWebRTCCodecsProxy::messageReceiverName());
 
     dispatchToThread([this, protectedThis = makeRef(*this)] {
+        auto locker = holdLock(m_lock);
         auto decoders = WTFMove(m_decoders);
         for (auto decoder : decoders.values())
             webrtc::releaseLocalDecoder(decoder);
@@ -73,45 +74,49 @@
 
 void LibWebRTCCodecsProxy::createH264Decoder(RTCDecoderIdentifier identifier)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(!m_decoders.contains(identifier));
     m_decoders.add(identifier, webrtc::createLocalH264Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
         if (auto sample = WebCore::RemoteVideoSample::create(pixelBuffer, MediaTime(timeStampNs, 1)))
             connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
     }).get()));
-    updateHasEncodersOrDecoders();
 }
 
 void LibWebRTCCodecsProxy::createH265Decoder(RTCDecoderIdentifier identifier)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(!m_decoders.contains(identifier));
     m_decoders.add(identifier, webrtc::createLocalH265Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
         if (auto sample = WebCore::RemoteVideoSample::create(pixelBuffer, MediaTime(timeStampNs, 1)))
             connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
     }).get()));
-    updateHasEncodersOrDecoders();
 }
 
 void LibWebRTCCodecsProxy::createVP9Decoder(RTCDecoderIdentifier identifier)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(!m_decoders.contains(identifier));
     m_decoders.add(identifier, webrtc::createLocalVP9Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
         if (auto sample = WebCore::RemoteVideoSample::create(pixelBuffer, MediaTime(timeStampNs, 1)))
             connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
     }).get()));
-    updateHasEncodersOrDecoders();
 }
 
 void LibWebRTCCodecsProxy::releaseDecoder(RTCDecoderIdentifier identifier)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(m_decoders.contains(identifier));
-    if (auto decoder = m_decoders.take(identifier)) {
+    if (auto decoder = m_decoders.take(identifier))
         webrtc::releaseLocalDecoder(decoder);
-        updateHasEncodersOrDecoders();
-    }
 }
 
 void LibWebRTCCodecsProxy::decodeFrame(RTCDecoderIdentifier identifier, uint32_t timeStamp, const IPC::DataReference& data)
 {
+    ASSERT(!isMainRunLoop());
     ASSERT(m_decoders.contains(identifier));
     auto decoder = m_decoders.get(identifier);
     if (!decoder)
@@ -123,6 +128,7 @@
 
 void LibWebRTCCodecsProxy::setFrameSize(RTCDecoderIdentifier identifier, uint16_t width, uint16_t height)
 {
+    ASSERT(!isMainRunLoop());
     ASSERT(m_decoders.contains(identifier));
     auto decoder = m_decoders.get(identifier);
     if (!decoder)
@@ -133,6 +139,8 @@
 
 void LibWebRTCCodecsProxy::createEncoder(RTCEncoderIdentifier identifier, const String& formatName, const Vector<std::pair<String, String>>& parameters, bool useLowLatency)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(!m_encoders.contains(identifier));
 
     std::map<std::string, std::string> rtcParameters;
@@ -144,20 +152,20 @@
     }).get());
     webrtc::setLocalEncoderLowLatency(encoder, useLowLatency);
     m_encoders.add(identifier, encoder);
-    updateHasEncodersOrDecoders();
 }
 
 void LibWebRTCCodecsProxy::releaseEncoder(RTCEncoderIdentifier identifier)
 {
+    ASSERT(!isMainRunLoop());
+    auto locker = holdLock(m_lock);
     ASSERT(m_encoders.contains(identifier));
-    if (auto encoder = m_encoders.take(identifier)) {
+    if (auto encoder = m_encoders.take(identifier))
         webrtc::releaseLocalEncoder(encoder);
-        updateHasEncodersOrDecoders();
-    }
 }
 
 void LibWebRTCCodecsProxy::initializeEncoder(RTCEncoderIdentifier identifier, uint16_t width, uint16_t height, unsigned startBitrate, unsigned maxBitrate, unsigned minBitrate, uint32_t maxFramerate)
 {
+    ASSERT(!isMainRunLoop());
     ASSERT(m_encoders.contains(identifier));
     auto encoder = m_encoders.get(identifier);
     if (!encoder)
@@ -184,6 +192,7 @@
 
 void LibWebRTCCodecsProxy::encodeFrame(RTCEncoderIdentifier identifier, WebCore::RemoteVideoSample&& sample, uint32_t timeStamp, bool shouldEncodeAsKeyFrame)
 {
+    ASSERT(!isMainRunLoop());
     ASSERT(m_encoders.contains(identifier));
     auto encoder = m_encoders.get(identifier);
     if (!encoder)
@@ -200,6 +209,7 @@
 
 void LibWebRTCCodecsProxy::setEncodeRates(RTCEncoderIdentifier identifier, uint32_t bitRate, uint32_t frameRate)
 {
+    ASSERT(!isMainRunLoop());
     auto encoder = m_encoders.get(identifier);
     if (!encoder)
         return;
@@ -207,14 +217,11 @@
     webrtc::setLocalEncoderRates(encoder, bitRate, frameRate);
 }
 
-void LibWebRTCCodecsProxy::updateHasEncodersOrDecoders()
-{
-    m_hasEncodersOrDecoders = !m_encoders.isEmpty() || !m_decoders.isEmpty();
-}
-
 bool LibWebRTCCodecsProxy::allowsExitUnderMemoryPressure() const
 {
-    return !m_hasEncodersOrDecoders;
+    ASSERT(isMainRunLoop());
+    auto locker = holdLock(m_lock);
+    return m_encoders.isEmpty() && m_decoders.isEmpty();
 }
 
 }
_______________________________________________
webkit-changes mailing list
[email protected]
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to