⚠ Archived content — this site is no longer maintained.   Current WebKit documentation is at docs.webkit.org.

Changeset 276223 in webkit


Ignore:
Timestamp:
Apr 18, 2021, 12:10:05 AM (5 years ago)
Author:
Chris Dumez
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.

Location:
trunk/Source/WebKit
Files:
3 edited

Legend:

Unmodified
Added
Removed
  • trunk/Source/WebKit/ChangeLog

    r276222 r276223  
     12021-04-18  Chris Dumez  <cdumez@apple.com>
     2
     3        Update LibWebRTCCodecsProxy to use a Lock
     4        https://bugs.webkit.org/show_bug.cgi?id=224728
     5
     6        Reviewed by Darin Adler.
     7
     8        Update LibWebRTCCodecsProxy to use a Lock, instead of a std::atomic<bool> that
     9        has to be kept up to date. I think this simplifies the code a bit. Adding / Removing
     10        encoder / decoder is not very hot code as far as I know and there will very rarely
     11        be contention since allowsExitUnderMemoryPressure() is only called on memory pressure.
     12
     13        m_encoder / m_decoder are still always modified from the background thread. However, we
     14        now check from the main thread if they are empty by locking.
     15
     16        * GPUProcess/webrtc/LibWebRTCCodecsProxy.h:
     17        * GPUProcess/webrtc/LibWebRTCCodecsProxy.mm:
     18        (WebKit::LibWebRTCCodecsProxy::close):
     19        (WebKit::LibWebRTCCodecsProxy::createH264Decoder):
     20        (WebKit::LibWebRTCCodecsProxy::createH265Decoder):
     21        (WebKit::LibWebRTCCodecsProxy::createVP9Decoder):
     22        (WebKit::LibWebRTCCodecsProxy::releaseDecoder):
     23        (WebKit::LibWebRTCCodecsProxy::decodeFrame):
     24        (WebKit::LibWebRTCCodecsProxy::setFrameSize):
     25        (WebKit::LibWebRTCCodecsProxy::createEncoder):
     26        (WebKit::LibWebRTCCodecsProxy::releaseEncoder):
     27        (WebKit::LibWebRTCCodecsProxy::initializeEncoder):
     28        (WebKit::LibWebRTCCodecsProxy::encodeFrame):
     29        (WebKit::LibWebRTCCodecsProxy::setEncodeRates):
     30        (WebKit::LibWebRTCCodecsProxy::allowsExitUnderMemoryPressure const):
     31        (WebKit::LibWebRTCCodecsProxy::updateHasEncodersOrDecoders): Deleted.
     32
    1332021-04-17  Chris Dumez  <cdumez@apple.com>
    234
  • trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.h

    r276222 r276223  
    8383    void setEncodeRates(RTCEncoderIdentifier, uint32_t bitRate, uint32_t frameRate);
    8484
    85     void updateHasEncodersOrDecoders();
    86 
    8785    CFDictionaryRef ioSurfacePixelBufferCreationOptions(IOSurfaceRef);
    8886
    8987    GPUConnectionToWebProcess& m_gpuConnectionToWebProcess;
     88
     89    mutable Lock m_lock;
    9090    HashMap<RTCDecoderIdentifier, webrtc::LocalDecoder> m_decoders;
    9191    HashMap<RTCEncoderIdentifier, webrtc::LocalEncoder> m_encoders;
    92     std::atomic<bool> m_hasEncodersOrDecoders;
    9392
    9493    Ref<WorkQueue> m_queue;
  • trunk/Source/WebKit/GPUProcess/webrtc/LibWebRTCCodecsProxy.mm

    r276222 r276223  
    6363
    6464    dispatchToThread([this, protectedThis = makeRef(*this)] {
     65        auto locker = holdLock(m_lock);
    6566        auto decoders = WTFMove(m_decoders);
    6667        for (auto decoder : decoders.values())
     
    7475void LibWebRTCCodecsProxy::createH264Decoder(RTCDecoderIdentifier identifier)
    7576{
     77    ASSERT(!isMainRunLoop());
     78    auto locker = holdLock(m_lock);
    7679    ASSERT(!m_decoders.contains(identifier));
    7780    m_decoders.add(identifier, webrtc::createLocalH264Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
     
    7982            connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
    8083    }).get()));
    81     updateHasEncodersOrDecoders();
    8284}
    8385
    8486void LibWebRTCCodecsProxy::createH265Decoder(RTCDecoderIdentifier identifier)
    8587{
     88    ASSERT(!isMainRunLoop());
     89    auto locker = holdLock(m_lock);
    8690    ASSERT(!m_decoders.contains(identifier));
    8791    m_decoders.add(identifier, webrtc::createLocalH265Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
     
    8993            connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
    9094    }).get()));
    91     updateHasEncodersOrDecoders();
    9295}
    9396
    9497void LibWebRTCCodecsProxy::createVP9Decoder(RTCDecoderIdentifier identifier)
    9598{
     99    ASSERT(!isMainRunLoop());
     100    auto locker = holdLock(m_lock);
    96101    ASSERT(!m_decoders.contains(identifier));
    97102    m_decoders.add(identifier, webrtc::createLocalVP9Decoder(makeBlockPtr([connection = makeRef(m_gpuConnectionToWebProcess.connection()), identifier](CVPixelBufferRef pixelBuffer, uint32_t timeStampNs, uint32_t timeStamp) {
     
    99104            connection->send(Messages::LibWebRTCCodecs::CompletedDecoding { identifier, timeStamp, *sample }, 0);
    100105    }).get()));
    101     updateHasEncodersOrDecoders();
    102106}
    103107
    104108void LibWebRTCCodecsProxy::releaseDecoder(RTCDecoderIdentifier identifier)
    105109{
     110    ASSERT(!isMainRunLoop());
     111    auto locker = holdLock(m_lock);
    106112    ASSERT(m_decoders.contains(identifier));
    107     if (auto decoder = m_decoders.take(identifier)) {
     113    if (auto decoder = m_decoders.take(identifier))
    108114        webrtc::releaseLocalDecoder(decoder);
    109         updateHasEncodersOrDecoders();
    110     }
    111115}
    112116
    113117void LibWebRTCCodecsProxy::decodeFrame(RTCDecoderIdentifier identifier, uint32_t timeStamp, const IPC::DataReference& data)
    114118{
     119    ASSERT(!isMainRunLoop());
    115120    ASSERT(m_decoders.contains(identifier));
    116121    auto decoder = m_decoders.get(identifier);
     
    124129void LibWebRTCCodecsProxy::setFrameSize(RTCDecoderIdentifier identifier, uint16_t width, uint16_t height)
    125130{
     131    ASSERT(!isMainRunLoop());
    126132    ASSERT(m_decoders.contains(identifier));
    127133    auto decoder = m_decoders.get(identifier);
     
    134140void LibWebRTCCodecsProxy::createEncoder(RTCEncoderIdentifier identifier, const String& formatName, const Vector<std::pair<String, String>>& parameters, bool useLowLatency)
    135141{
     142    ASSERT(!isMainRunLoop());
     143    auto locker = holdLock(m_lock);
    136144    ASSERT(!m_encoders.contains(identifier));
    137145
     
    145153    webrtc::setLocalEncoderLowLatency(encoder, useLowLatency);
    146154    m_encoders.add(identifier, encoder);
    147     updateHasEncodersOrDecoders();
    148155}
    149156
    150157void LibWebRTCCodecsProxy::releaseEncoder(RTCEncoderIdentifier identifier)
    151158{
     159    ASSERT(!isMainRunLoop());
     160    auto locker = holdLock(m_lock);
    152161    ASSERT(m_encoders.contains(identifier));
    153     if (auto encoder = m_encoders.take(identifier)) {
     162    if (auto encoder = m_encoders.take(identifier))
    154163        webrtc::releaseLocalEncoder(encoder);
    155         updateHasEncodersOrDecoders();
    156     }
    157164}
    158165
    159166void LibWebRTCCodecsProxy::initializeEncoder(RTCEncoderIdentifier identifier, uint16_t width, uint16_t height, unsigned startBitrate, unsigned maxBitrate, unsigned minBitrate, uint32_t maxFramerate)
    160167{
     168    ASSERT(!isMainRunLoop());
    161169    ASSERT(m_encoders.contains(identifier));
    162170    auto encoder = m_encoders.get(identifier);
     
    185193void LibWebRTCCodecsProxy::encodeFrame(RTCEncoderIdentifier identifier, WebCore::RemoteVideoSample&& sample, uint32_t timeStamp, bool shouldEncodeAsKeyFrame)
    186194{
     195    ASSERT(!isMainRunLoop());
    187196    ASSERT(m_encoders.contains(identifier));
    188197    auto encoder = m_encoders.get(identifier);
     
    201210void LibWebRTCCodecsProxy::setEncodeRates(RTCEncoderIdentifier identifier, uint32_t bitRate, uint32_t frameRate)
    202211{
     212    ASSERT(!isMainRunLoop());
    203213    auto encoder = m_encoders.get(identifier);
    204214    if (!encoder)
     
    208218}
    209219
    210 void LibWebRTCCodecsProxy::updateHasEncodersOrDecoders()
    211 {
    212     m_hasEncodersOrDecoders = !m_encoders.isEmpty() || !m_decoders.isEmpty();
    213 }
    214 
    215220bool LibWebRTCCodecsProxy::allowsExitUnderMemoryPressure() const
    216221{
    217     return !m_hasEncodersOrDecoders;
     222    ASSERT(isMainRunLoop());
     223    auto locker = holdLock(m_lock);
     224    return m_encoders.isEmpty() && m_decoders.isEmpty();
    218225}
    219226
Note: See TracChangeset for help on using the changeset viewer.