Title: [247238] trunk/Source/WebCore
Revision
247238
Author
you...@apple.com
Date
2019-07-08 16:51:43 -0700 (Mon, 08 Jul 2019)

Log Message

Hop explicitly to the main thread after generating a frame in ScreenDisplayCaptureSourceMac
https://bugs.webkit.org/show_bug.cgi?id=199581

Reviewed by Eric Carlson.

Instead of locking and setting the current frame from a background thread, hop to the main thread.
This also makes sure the weakThis check is done in the main thread.
Manually tested.

* platform/mediastream/mac/ScreenDisplayCaptureSourceMac.h:
(WebCore::ScreenDisplayCaptureSourceMac::DisplaySurface::DisplaySurface):
* platform/mediastream/mac/ScreenDisplayCaptureSourceMac.mm:
(WebCore::ScreenDisplayCaptureSourceMac::createDisplayStream):
(WebCore::ScreenDisplayCaptureSourceMac::generateFrame):
(WebCore::ScreenDisplayCaptureSourceMac::newFrame):
(WebCore::ScreenDisplayCaptureSourceMac::frameAvailable): Deleted.

Modified Paths

Diff

Modified: trunk/Source/WebCore/ChangeLog (247237 => 247238)


--- trunk/Source/WebCore/ChangeLog	2019-07-08 23:49:10 UTC (rev 247237)
+++ trunk/Source/WebCore/ChangeLog	2019-07-08 23:51:43 UTC (rev 247238)
@@ -1,3 +1,22 @@
+2019-07-08  Youenn Fablet  <you...@apple.com>
+
+        Hop explicitly to the main thread after generating a frame in ScreenDisplayCaptureSourceMac
+        https://bugs.webkit.org/show_bug.cgi?id=199581
+
+        Reviewed by Eric Carlson.
+
+        Instead of locking and setting the current frame from a background thread, hop to the main thread.
+        This also makes sure the weakThis check is done in the main thread.
+        Manually tested.
+
+        * platform/mediastream/mac/ScreenDisplayCaptureSourceMac.h:
+        (WebCore::ScreenDisplayCaptureSourceMac::DisplaySurface::DisplaySurface):
+        * platform/mediastream/mac/ScreenDisplayCaptureSourceMac.mm:
+        (WebCore::ScreenDisplayCaptureSourceMac::createDisplayStream):
+        (WebCore::ScreenDisplayCaptureSourceMac::generateFrame):
+        (WebCore::ScreenDisplayCaptureSourceMac::newFrame):
+        (WebCore::ScreenDisplayCaptureSourceMac::frameAvailable): Deleted.
+
 2019-07-08  Daniel Bates  <daba...@apple.com>
 
         Command + . generates Escape with key identifier Period, should be Escape

Modified: trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.h (247237 => 247238)


--- trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.h	2019-07-08 23:49:10 UTC (rev 247237)
+++ trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.h	2019-07-08 23:51:43 UTC (rev 247238)
@@ -54,8 +54,6 @@
 
     void displayWasReconfigured(CGDirectDisplayID, CGDisplayChangeSummaryFlags);
 
-    void frameAvailable(CGDisplayStreamFrameStatus, uint64_t, IOSurfaceRef, CGDisplayStreamUpdateRef);
-
     DisplayCaptureSourceCocoa::DisplayFrameType generateFrame() final;
     RealtimeMediaSourceSettings::DisplaySurfaceType surfaceType() const final { return RealtimeMediaSourceSettings::DisplaySurfaceType::Monitor; }
 
@@ -74,6 +72,13 @@
     class DisplaySurface {
     public:
         DisplaySurface() = default;
+        explicit DisplaySurface(IOSurfaceRef surface)
+            : m_surface(surface)
+        {
+            if (m_surface)
+                IOSurfaceIncrementUseCount(m_surface.get());
+        }
+
         ~DisplaySurface()
         {
             if (m_surface)
@@ -96,7 +101,8 @@
         RetainPtr<IOSurfaceRef> m_surface;
     };
 
-    mutable Lock m_currentFrameMutex;
+    void newFrame(CGDisplayStreamFrameStatus, DisplaySurface&&);
+
     DisplaySurface m_currentFrame;
     RetainPtr<CGDisplayStreamRef> m_displayStream;
     OSObjectPtr<dispatch_queue_t> m_captureQueue;

Modified: trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.mm (247237 => 247238)


--- trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.mm	2019-07-08 23:49:10 UTC (rev 247237)
+++ trunk/Source/WebCore/platform/mediastream/mac/ScreenDisplayCaptureSourceMac.mm	2019-07-08 23:51:43 UTC (rev 247238)
@@ -154,10 +154,20 @@
 
         auto weakThis = makeWeakPtr(*this);
         auto frameAvailableBlock = ^(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef) {
-            if (!weakThis)
+
+            if (!frameSurface || !displayTime)
                 return;
 
-            weakThis->frameAvailable(status, displayTime, frameSurface, updateRef);
+            size_t count;
+            auto* rects = CGDisplayStreamUpdateGetRects(updateRef, kCGDisplayStreamUpdateDirtyRects, &count);
+            if (!rects || !count)
+                return;
+
+            RunLoop::main().dispatch([weakThis, status, frame = DisplaySurface { frameSurface }]() mutable {
+                if (!weakThis)
+                    return;
+                weakThis->newFrame(status, WTFMove(frame));
+            });
         };
 
         m_displayStream = adoptCF(CGDisplayStreamCreateWithDispatchQueue(m_displayID, screenWidth, screenHeight, preferedPixelBufferFormat(), (__bridge CFDictionaryRef)streamOptions, m_captureQueue.get(), frameAvailableBlock));
@@ -203,13 +213,7 @@
 
 DisplayCaptureSourceCocoa::DisplayFrameType ScreenDisplayCaptureSourceMac::generateFrame()
 {
-    DisplaySurface currentFrame;
-    {
-        LockHolder lock(m_currentFrameMutex);
-        currentFrame = m_currentFrame.ioSurface();
-    }
-
-    return DisplayCaptureSourceCocoa::DisplayFrameType { RetainPtr<IOSurfaceRef> { currentFrame.ioSurface() } };
+    return DisplayCaptureSourceCocoa::DisplayFrameType { RetainPtr<IOSurfaceRef> { m_currentFrame.ioSurface() } };
 }
 
 void ScreenDisplayCaptureSourceMac::startDisplayStream()
@@ -253,7 +257,7 @@
         reinterpret_cast<ScreenDisplayCaptureSourceMac *>(userInfo)->displayWasReconfigured(display, flags);
 }
 
-void ScreenDisplayCaptureSourceMac::frameAvailable(CGDisplayStreamFrameStatus status, uint64_t displayTime, IOSurfaceRef frameSurface, CGDisplayStreamUpdateRef updateRef)
+void ScreenDisplayCaptureSourceMac::newFrame(CGDisplayStreamFrameStatus status, DisplaySurface&& newFrame)
 {
     switch (status) {
     case kCGDisplayStreamFrameStatusFrameComplete:
@@ -271,16 +275,7 @@
         break;
     }
 
-    if (!frameSurface || !displayTime)
-        return;
-
-    size_t count;
-    auto* rects = CGDisplayStreamUpdateGetRects(updateRef, kCGDisplayStreamUpdateDirtyRects, &count);
-    if (!rects || !count)
-        return;
-
-    LockHolder lock(m_currentFrameMutex);
-    m_currentFrame = frameSurface;
+    m_currentFrame = WTFMove(newFrame);
 }
 
 Optional<CaptureDevice> ScreenDisplayCaptureSourceMac::screenCaptureDeviceWithPersistentID(const String& deviceID)
_______________________________________________
webkit-changes mailing list
webkit-changes@lists.webkit.org
https://lists.webkit.org/mailman/listinfo/webkit-changes

Reply via email to