2016-10-13 15:45:03 +03:00
|
|
|
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
|
|
|
|
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
|
|
|
|
/* This Source Code Form is subject to the terms of the Mozilla Public
|
|
|
|
* License, v. 2.0. If a copy of the MPL was not distributed with this
|
|
|
|
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
|
|
|
|
|
|
|
|
/* some utilities for stylo */
|
|
|
|
|
|
|
|
#ifndef mozilla_ServoUtils_h
|
|
|
|
#define mozilla_ServoUtils_h
|
|
|
|
|
2018-01-31 01:46:33 +03:00
|
|
|
#include "mozilla/Assertions.h"
|
2016-10-13 15:45:03 +03:00
|
|
|
#include "mozilla/TypeTraits.h"
|
2018-01-30 23:05:43 +03:00
|
|
|
#include "MainThreadUtils.h"
|
2016-10-13 15:45:03 +03:00
|
|
|
|
2017-04-30 08:15:42 +03:00
|
|
|
namespace mozilla {
|
|
|
|
|
|
|
|
// Defined in ServoBindings.cpp.
|
|
|
|
void AssertIsMainThreadOrServoFontMetricsLocked();
|
|
|
|
|
2018-01-30 23:05:43 +03:00
|
|
|
class ServoStyleSet;
|
|
|
|
extern ServoStyleSet* sInServoTraversal;
|
|
|
|
inline bool IsInServoTraversal()
|
|
|
|
{
|
|
|
|
// The callers of this function are generally main-thread-only _except_
|
|
|
|
// for potentially running during the Servo traversal, in which case they may
|
|
|
|
// take special paths that avoid writing to caches and the like. In order
|
|
|
|
// to allow those callers to branch efficiently without checking TLS, we
|
|
|
|
// maintain this static boolean. However, the danger is that those callers
|
|
|
|
// are generally unprepared to deal with non-Servo-but-also-non-main-thread
|
|
|
|
// callers, and are likely to take the main-thread codepath if this function
|
|
|
|
// returns false. So we assert against other non-main-thread callers here.
|
|
|
|
MOZ_ASSERT(sInServoTraversal || NS_IsMainThread());
|
|
|
|
return sInServoTraversal;
|
|
|
|
}
|
2017-04-30 08:15:42 +03:00
|
|
|
} // namespace mozilla
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#if defined(MOZ_STYLO) && defined(MOZ_OLD_STYLE)
|
2016-10-13 15:45:03 +03:00
|
|
|
# define MOZ_DECL_STYLO_CHECK_METHODS \
|
|
|
|
bool IsGecko() const { return !IsServo(); } \
|
|
|
|
bool IsServo() const { return mType == StyleBackendType::Servo; }
|
2018-02-01 07:04:04 +03:00
|
|
|
#elif defined(MOZ_STYLO)
|
|
|
|
# define MOZ_DECL_STYLO_CHECK_METHODS \
|
|
|
|
bool IsGecko() const { return false; } \
|
|
|
|
bool IsServo() const { return true; }
|
2016-10-13 15:45:03 +03:00
|
|
|
#else
|
|
|
|
# define MOZ_DECL_STYLO_CHECK_METHODS \
|
|
|
|
bool IsGecko() const { return true; } \
|
|
|
|
bool IsServo() const { return false; }
|
|
|
|
#endif
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#define MOZ_DECL_STYLO_CONVERT_METHODS_SERVO(servotype_) \
|
2016-10-13 15:45:03 +03:00
|
|
|
inline servotype_* AsServo(); \
|
2017-02-13 06:21:33 +03:00
|
|
|
inline const servotype_* AsServo() const; \
|
|
|
|
inline servotype_* GetAsServo(); \
|
|
|
|
inline const servotype_* GetAsServo() const;
|
2016-10-13 15:45:03 +03:00
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#define MOZ_DECL_STYLO_CONVERT_METHODS_GECKO(geckotype_) \
|
|
|
|
inline geckotype_* AsGecko(); \
|
|
|
|
inline const geckotype_* AsGecko() const; \
|
|
|
|
inline geckotype_* GetAsGecko(); \
|
|
|
|
inline const geckotype_* GetAsGecko() const;
|
|
|
|
|
|
|
|
#ifdef MOZ_OLD_STYLE
|
|
|
|
#define MOZ_DECL_STYLO_CONVERT_METHODS(geckotype_, servotype_) \
|
|
|
|
MOZ_DECL_STYLO_CONVERT_METHODS_SERVO(servotype_) \
|
|
|
|
MOZ_DECL_STYLO_CONVERT_METHODS_GECKO(geckotype_)
|
|
|
|
#else
|
|
|
|
#define MOZ_DECL_STYLO_CONVERT_METHODS(geckotype_, servotype_) \
|
|
|
|
MOZ_DECL_STYLO_CONVERT_METHODS_SERVO(servotype_)
|
|
|
|
#endif
|
|
|
|
|
2017-06-11 08:27:45 +03:00
|
|
|
/**
|
|
|
|
* Macro used in a base class of |geckotype_| and |servotype_|.
|
|
|
|
* The class should define |StyleBackendType mType;| itself.
|
|
|
|
*/
|
|
|
|
#define MOZ_DECL_STYLO_METHODS(geckotype_, servotype_) \
|
|
|
|
MOZ_DECL_STYLO_CHECK_METHODS \
|
|
|
|
MOZ_DECL_STYLO_CONVERT_METHODS(geckotype_, servotype_)
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
#define MOZ_DEFINE_STYLO_METHODS_GECKO(type_, geckotype_) \
|
2016-10-13 15:45:03 +03:00
|
|
|
geckotype_* type_::AsGecko() { \
|
2017-08-08 22:04:31 +03:00
|
|
|
MOZ_ASSERT(IsGecko()); \
|
2016-10-13 15:45:03 +03:00
|
|
|
return static_cast<geckotype_*>(this); \
|
|
|
|
} \
|
|
|
|
const geckotype_* type_::AsGecko() const { \
|
2017-08-08 22:04:31 +03:00
|
|
|
MOZ_ASSERT(IsGecko()); \
|
2016-10-13 15:45:03 +03:00
|
|
|
return static_cast<const geckotype_*>(this); \
|
|
|
|
} \
|
2018-02-01 07:04:04 +03:00
|
|
|
geckotype_* type_::GetAsGecko() { \
|
|
|
|
return IsGecko() ? AsGecko() : nullptr; \
|
|
|
|
} \
|
|
|
|
const geckotype_* type_::GetAsGecko() const { \
|
|
|
|
return IsGecko() ? AsGecko() : nullptr; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MOZ_DEFINE_STYLO_METHODS_SERVO(type_, servotype_) \
|
|
|
|
servotype_* type_::AsServo() { \
|
|
|
|
MOZ_ASSERT(IsServo()); \
|
|
|
|
return static_cast<servotype_*>(this); \
|
|
|
|
} \
|
2016-10-13 15:45:03 +03:00
|
|
|
const servotype_* type_::AsServo() const { \
|
2017-08-08 22:04:31 +03:00
|
|
|
MOZ_ASSERT(IsServo()); \
|
2016-10-13 15:45:03 +03:00
|
|
|
return static_cast<const servotype_*>(this); \
|
2017-02-13 06:21:33 +03:00
|
|
|
} \
|
|
|
|
servotype_* type_::GetAsServo() { \
|
|
|
|
return IsServo() ? AsServo() : nullptr; \
|
|
|
|
} \
|
|
|
|
const servotype_* type_::GetAsServo() const { \
|
|
|
|
return IsServo() ? AsServo() : nullptr; \
|
2016-10-13 15:45:03 +03:00
|
|
|
}
|
|
|
|
|
2018-02-01 07:04:04 +03:00
|
|
|
|
|
|
|
/**
|
|
|
|
* Macro used in inline header of class |type_| with its Gecko and Servo
|
|
|
|
* subclasses named |geckotype_| and |servotype_| correspondingly for
|
|
|
|
* implementing the inline methods defined by MOZ_DECL_STYLO_METHODS.
|
|
|
|
*/
|
|
|
|
#ifdef MOZ_OLD_STYLE
|
|
|
|
#define MOZ_DEFINE_STYLO_METHODS(type_, geckotype_, servotype_) \
|
|
|
|
MOZ_DEFINE_STYLO_METHODS_SERVO(type_, servotype_) \
|
|
|
|
MOZ_DEFINE_STYLO_METHODS_GECKO(type_, geckotype_)
|
|
|
|
#else
|
|
|
|
#define MOZ_DEFINE_STYLO_METHODS(type_, geckotype_, servotype_) \
|
|
|
|
MOZ_DEFINE_STYLO_METHODS_SERVO(type_, servotype_)
|
|
|
|
#endif
|
|
|
|
|
2016-10-13 15:45:03 +03:00
|
|
|
#define MOZ_STYLO_THIS_TYPE mozilla::RemovePointer<decltype(this)>::Type
|
|
|
|
#define MOZ_STYLO_GECKO_TYPE mozilla::RemovePointer<decltype(AsGecko())>::Type
|
|
|
|
#define MOZ_STYLO_SERVO_TYPE mozilla::RemovePointer<decltype(AsServo())>::Type
|
|
|
|
|
|
|
|
/**
|
|
|
|
* Macro used to forward a method call to the concrete method defined by
|
|
|
|
* the Servo or Gecko implementation. The class of the method using it
|
|
|
|
* should use MOZ_DECL_STYLO_METHODS to define basic stylo methods.
|
|
|
|
*/
|
2018-02-01 07:04:04 +03:00
|
|
|
#ifdef MOZ_OLD_STYLE
|
2016-10-13 15:45:03 +03:00
|
|
|
#define MOZ_STYLO_FORWARD_CONCRETE(method_, geckoargs_, servoargs_) \
|
|
|
|
static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_), \
|
|
|
|
decltype(&MOZ_STYLO_GECKO_TYPE::method_)> \
|
|
|
|
::value, "Gecko subclass should define its own " #method_); \
|
|
|
|
static_assert(!mozilla::IsSame<decltype(&MOZ_STYLO_THIS_TYPE::method_), \
|
|
|
|
decltype(&MOZ_STYLO_SERVO_TYPE::method_)> \
|
|
|
|
::value, "Servo subclass should define its own " #method_); \
|
|
|
|
if (IsServo()) { \
|
|
|
|
return AsServo()->method_ servoargs_; \
|
|
|
|
} \
|
|
|
|
return AsGecko()->method_ geckoargs_;
|
2018-02-01 07:04:04 +03:00
|
|
|
#else
|
|
|
|
#define MOZ_STYLO_FORWARD_CONCRETE(method_, geckoargs_, servoargs_) \
|
|
|
|
return AsServo()->method_ servoargs_;
|
|
|
|
#endif
|
2016-10-13 15:45:03 +03:00
|
|
|
|
|
|
|
#define MOZ_STYLO_FORWARD(method_, args_) \
|
|
|
|
MOZ_STYLO_FORWARD_CONCRETE(method_, args_, args_)
|
|
|
|
|
2017-01-31 10:10:45 +03:00
|
|
|
// Warning in MOZ_STYLO builds and non-fatally assert in regular builds.
|
|
|
|
#define NS_ASSERTION_STYLO_WARNING_EXPAND(X) X
|
|
|
|
#ifdef MOZ_STYLO
|
|
|
|
#define NS_ASSERTION_STYLO_WARNING(...) \
|
|
|
|
NS_ASSERTION_STYLO_WARNING_EXPAND(NS_WARNING_ASSERTION(__VA_ARGS__))
|
|
|
|
#else
|
|
|
|
#define NS_ASSERTION_STYLO_WARNING(...) \
|
|
|
|
NS_ASSERTION_STYLO_WARNING_EXPAND(NS_ASSERTION(__VA_ARGS__))
|
|
|
|
#endif
|
|
|
|
|
2016-10-13 15:45:03 +03:00
|
|
|
#endif // mozilla_ServoUtils_h
|