gfx/config/gfxConfig.cpp
author Boris Chiou <boris.chiou@gmail.com>
Wed, 25 May 2016 17:10:53 +0800
changeset 371285 f3290344ee2948b63d3ea7b9f20e3d6e4a3c6671
parent 357322 1d7eeee5a5c559166c166e726863ab3e73f78e9f
child 367028 5df88530353c4c757db4c5fb965a12d3f9aca4c9
child 368483 affc0f3797381ffdd8c6f2fb21a61c96092df504
child 370471 c7d9c4795b0b04d00830c802874a6806f3eac302
child 371546 b6969b655923b20ba21693ce514f5ad2bbc2c97d
permissions -rw-r--r--
Bug 1244590 - Part 6: Refactor the calculation of StyleAnimationValue. We want to reuse the StyleAnimationValues of properties of each keyframe when applying spacing and building animation property segments, so refactor this part. MozReview-Commit-ID: 8G56C3BU3FR

/* -*- Mode: C++; indent-tabs-mode: nil; c-basic-offset: 4 -*- */
/* vim: set sts=2 ts=8 sw=2 tw=99 et: */
/* 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/. */
#include "gfxConfig.h"
#include "plstr.h"

namespace mozilla {
namespace gfx {

static gfxConfig sConfig;

/* static */ FeatureState&
gfxConfig::GetFeature(Feature aFeature)
{
  return sConfig.GetState(aFeature);
}

/* static */ bool
gfxConfig::IsEnabled(Feature aFeature)
{
  const FeatureState& state = sConfig.GetState(aFeature);
  return state.IsEnabled();
}

/* static */ bool
gfxConfig::IsDisabledByDefault(Feature aFeature)
{
  const FeatureState& state = sConfig.GetState(aFeature);
  return state.DisabledByDefault();
}

/* static */ bool
gfxConfig::IsForcedOnByUser(Feature aFeature)
{
  const FeatureState& state = sConfig.GetState(aFeature);
  return state.IsForcedOnByUser();
}

/* static */ FeatureStatus
gfxConfig::GetValue(Feature aFeature)
{
  const FeatureState& state = sConfig.GetState(aFeature);
  return state.GetValue();
}

/* static */ bool
gfxConfig::SetDefault(Feature aFeature,
                      bool aEnable,
                      FeatureStatus aDisableStatus,
                      const char* aDisableMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  return state.SetDefault(aEnable, aDisableStatus, aDisableMessage);
}

/* static */ void
gfxConfig::DisableByDefault(Feature aFeature,
                            FeatureStatus aDisableStatus,
                            const char* aDisableMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.DisableByDefault(aDisableStatus, aDisableMessage);
}

/* static */ void
gfxConfig::EnableByDefault(Feature aFeature)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.EnableByDefault();
}

/* static */ void
gfxConfig::SetDefaultFromPref(Feature aFeature,
                              const char* aPrefName,
                              bool aIsEnablePref,
                              bool aDefaultValue)
{
  FeatureState& state = sConfig.GetState(aFeature);
  return state.SetDefaultFromPref(aPrefName, aIsEnablePref, aDefaultValue);
}

/* static */ bool
gfxConfig::InitOrUpdate(Feature aFeature,
                        bool aEnable,
                        FeatureStatus aDisableStatus,
                        const char* aDisableMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  return state.InitOrUpdate(aEnable, aDisableStatus, aDisableMessage);
}

/* static */ void
gfxConfig::SetFailed(Feature aFeature, FeatureStatus aStatus, const char* aMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.SetFailed(aStatus, aMessage);
}

/* static */ void
gfxConfig::Disable(Feature aFeature, FeatureStatus aStatus, const char* aMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.Disable(aStatus, aMessage);
}

/* static */ void
gfxConfig::UserEnable(Feature aFeature, const char* aMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.UserEnable(aMessage);
}

/* static */ void
gfxConfig::UserForceEnable(Feature aFeature, const char* aMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.UserForceEnable(aMessage);
}

/* static */ void
gfxConfig::UserDisable(Feature aFeature, const char* aMessage)
{
  FeatureState& state = sConfig.GetState(aFeature);
  state.UserDisable(aMessage);
}

/* static */ void
gfxConfig::Reenable(Feature aFeature, Fallback aFallback)
{
  FeatureState& state = sConfig.GetState(aFeature);
  MOZ_ASSERT(IsFeatureStatusFailure(state.GetValue()));

  const char* message = state.GetRuntimeMessage();
  EnableFallback(aFallback, message);
  state.SetRuntime(FeatureStatus::Available, nullptr);
}

/* static */ bool
gfxConfig::UseFallback(Fallback aFallback)
{
  return sConfig.UseFallbackImpl(aFallback);
}

/* static */ void
gfxConfig::EnableFallback(Fallback aFallback, const char* aMessage)
{
  // Ignore aMessage for now.
  sConfig.EnableFallbackImpl(aFallback, aMessage);
}

bool
gfxConfig::UseFallbackImpl(Fallback aFallback) const
{
  return !!(mFallbackBits & (uint64_t(1) << uint64_t(aFallback)));
}

void
gfxConfig::EnableFallbackImpl(Fallback aFallback, const char* aMessage)
{
  if (!UseFallbackImpl(aFallback)) {
    MOZ_ASSERT(mNumFallbackLogEntries < kNumFallbacks);

    FallbackLogEntry& entry = mFallbackLog[mNumFallbackLogEntries];
    mNumFallbackLogEntries++;

    entry.mFallback = aFallback;
    PL_strncpyz(entry.mMessage, aMessage, sizeof(entry.mMessage));
  }
  mFallbackBits |= (uint64_t(1) << uint64_t(aFallback));
}

struct FeatureInfo {
  const char* name;
  const char* description;
};
static const FeatureInfo sFeatureInfo[] = {
#define FOR_EACH_FEATURE(name, type, desc) {#name, desc},
  GFX_FEATURE_MAP(FOR_EACH_FEATURE)
#undef FOR_EACH_FEATURE
  {nullptr, nullptr}
};

/* static */ void
gfxConfig::ForEachFeature(const FeatureIterCallback& aCallback)
{
  for (size_t i = 0; i < kNumFeatures; i++) {
    FeatureState& state = GetFeature(static_cast<Feature>(i));
    if (!state.IsInitialized()) {
      continue;
    }

    aCallback(sFeatureInfo[i].name,
              sFeatureInfo[i].description,
              state);
  }
}

static const char* sFallbackNames[] = {
#define FOR_EACH_FALLBACK(name) #name,
  GFX_FALLBACK_MAP(FOR_EACH_FALLBACK)
#undef FOR_EACH_FALLBACK
  nullptr
};

/* static  */ void
gfxConfig::ForEachFallback(const FallbackIterCallback& aCallback)
{
  sConfig.ForEachFallbackImpl(aCallback);
}

void
gfxConfig::ForEachFallbackImpl(const FallbackIterCallback& aCallback)
{
  for (size_t i = 0; i < mNumFallbackLogEntries; i++) {
    const FallbackLogEntry& entry = mFallbackLog[i];
    aCallback(sFallbackNames[size_t(entry.mFallback)], entry.mMessage);
  }
}

} // namespace gfx
} // namespace mozilla