Bug 1356103 - Part 5: Allow access to WeakPtr<UnscaledFont> when the Servo font metrics mutex is locked. r=lsalzman draft
authorCameron McCormack <cam@mcc.id.au>
Sun, 30 Apr 2017 14:47:22 +0800
changeset 572577 281e0b2118e6e79a6756636fff7373f316894c44
parent 572576 e4c231fa09ec1eb15db9b4a8b40385bb6971d3f2
child 572578 b7f5298203aa0281b656198c209d7b18cfee62ac
push id57115
push userbmo:cam@mcc.id.au
push dateThu, 04 May 2017 10:01:25 +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;