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
--- 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;