Bug 1356103 - Part 5: Allow access to WeakPtr<UnscaledFont> when the Servo font metrics mutex is locked. r=lsalzman
authorCameron McCormack <cam@mcc.id.au>
Sun, 30 Apr 2017 14:47:22 +0800
changeset 573133 94644351685237ce3b9518da3fa515f0d1f307d8
parent 573132 17ead22432966325310e4855d008fd0c2f63dfa4
child 573134 f2ca4b7b166f52401887af813add144412a053e3
push id57306
push userbmo:emilio+bugs@crisal.io
push dateFri, 05 May 2017 10:08:55 +0000
reviewerslsalzman
bugs1356103
milestone55.0a1
Bug 1356103 - Part 5: Allow access to WeakPtr<UnscaledFont> when the Servo font metrics mutex is locked. r=lsalzman We need to grab UnscaledFont objects through WeakPtrs during metrics calculations, but this only happens on Servo style worker threads while the Servo font metrics mutex is locked (and while the main thread is paused). So we use WeakPtrTraits to override the thread safety assertion to allow OMT access through the WeakPtr if the mutex is locked. Since we can end up creating UnscaledFont objects from the style worker threads too, we additionally need to allow the main thread to access them through WeakPtrs. It would be nice to avoid the dependency on layout/style/ here, but I can't see a way to do that without putting the burden of the annotation that the more permissive thread safety checks are allowed on the WeakPtr<> declarations themselves. MozReview-Commit-ID: AbHNZEhE7L8
gfx/2d/2D.h
--- a/gfx/2d/2D.h
+++ b/gfx/2d/2D.h
@@ -21,16 +21,17 @@
 // to be able to hold on to a GLContext.
 #include "mozilla/GenericRefCounted.h"
 #include "mozilla/MemoryReporting.h"
 
 // This RefPtr class isn't ideal for usage in Azure, as it doesn't allow T**
 // outparams using the &-operator. But it will have to do as there's no easy
 // solution.
 #include "mozilla/RefPtr.h"
+#include "mozilla/ServoUtils.h"
 #include "mozilla/WeakPtr.h"
 
 #include "mozilla/DebugOnly.h"
 
 #ifdef MOZ_ENABLE_FREETYPE
 #include <string>
 #endif
 
@@ -58,16 +59,33 @@ class SkCanvas;
 struct gfxFontStyle;
 
 struct CGContext;
 typedef struct CGContext *CGContextRef;
 
 namespace mozilla {
 
 namespace gfx {
+class UnscaledFont;
+}
+
+template<>
+struct WeakPtrTraits<gfx::UnscaledFont>
+{
+  static void AssertSafeToAccessFromNonOwningThread()
+  {
+    // We want to allow UnscaledFont objects that were created on the main
+    // thread to be accessed from other threads if the Servo font metrics
+    // mutex is locked, and for objects created on Servo style worker threads
+    // to be accessed later back on the main thread.
+    AssertIsMainThreadOrServoFontMetricsLocked();
+  }
+};
+
+namespace gfx {
 
 class ScaledFont;
 class SourceSurface;
 class DataSourceSurface;
 class DrawTarget;
 class DrawEventRecorder;
 class FilterNode;
 class LogForwarder;