Global Metrics

path: .metrics.mi.mi_original
old: 26.554801239505323
new: -25.247638640227436

path: .metrics.mi.mi_sei
old: -12.731422063166413
new: -74.65514418594913

path: .metrics.mi.mi_visual_studio
old: 15.529123531874458
new: 0.0

path: .metrics.cyclomatic.sum
old: 15.0
new: 152.0

path: .metrics.cyclomatic.average
old: 1.6666666666666667
new: 2.235294117647059

path: .metrics.cognitive.average
old: 1.8571428571428572
new: 2.15625

path: .metrics.cognitive.sum
old: 13.0
new: 138.0

path: .metrics.nargs.sum
old: 12.0
new: 54.0

path: .metrics.nargs.average
old: 1.7142857142857142
new: 0.84375

path: .metrics.loc.sloc
old: 332.0
new: 833.0

path: .metrics.loc.lloc
old: 89.0
new: 293.0

path: .metrics.loc.cloc
old: 32.0
new: 72.0

path: .metrics.loc.ploc
old: 257.0
new: 636.0

path: .metrics.loc.blank
old: 43.0
new: 125.0

path: .metrics.halstead.N1
old: 549.0
new: 1667.0

path: .metrics.halstead.time
old: 18243.03877653608
new: 64774.31782673867

path: .metrics.halstead.length
old: 1100.0
new: 2745.0

path: .metrics.halstead.bugs
old: 1.5865674358293966
new: 3.6925720714903343

path: .metrics.halstead.volume
old: 8343.458750793272
new: 23524.253644868448

path: .metrics.halstead.n1
old: 24.0
new: 32.0

path: .metrics.halstead.n2
old: 168.0
new: 348.0

path: .metrics.halstead.N2
old: 551.0
new: 1078.0

path: .metrics.halstead.effort
old: 328374.69797764946
new: 1165937.720881296

path: .metrics.halstead.purity_ratio
old: 1.2290440245855814
new: 1.1286500315320065

path: .metrics.halstead.difficulty
old: 39.357142857142854
new: 49.5632183908046

path: .metrics.halstead.estimated_program_length
old: 1351.9484270441394
new: 3098.1443365553578

path: .metrics.halstead.vocabulary
old: 192.0
new: 380.0

path: .metrics.halstead.level
old: 0.025408348457350273
new: 0.02017625231910946

path: .metrics.nexits.sum
old: 2.0
new: 81.0

path: .metrics.nexits.average
old: 0.2857142857142857
new: 1.265625

path: .metrics.nom.functions
old: 7.0
new: 64.0

path: .metrics.nom.total
old: 7.0
new: 64.0

Spaces Data

Minimal test - lines (21, 833)

path: .spaces[0].metrics.cyclomatic.average
old: 1.0
new: 2.253731343283582

path: .spaces[0].metrics.cyclomatic.sum
old: 2.0
new: 151.0

path: .spaces[0].metrics.cognitive.sum
old: 0.0
new: 138.0

path: .spaces[0].metrics.cognitive.average
old: 0.0
new: 2.15625

path: .spaces[0].metrics.nargs.sum
old: 0.0
new: 54.0

path: .spaces[0].metrics.nargs.average
old: 0.0
new: 0.84375

path: .spaces[0].metrics.loc.sloc
old: 18.0
new: 813.0

path: .spaces[0].metrics.loc.cloc
old: 2.0
new: 67.0

path: .spaces[0].metrics.loc.lloc
old: 3.0
new: 293.0

path: .spaces[0].metrics.loc.blank
old: 2.0
new: 122.0

path: .spaces[0].metrics.loc.ploc
old: 14.0
new: 624.0

path: .spaces[0].metrics.halstead.vocabulary
old: 33.0
new: 368.0

path: .spaces[0].metrics.halstead.estimated_program_length
old: 135.25821588700785
new: 2979.8186540536635

path: .spaces[0].metrics.halstead.n1
old: 12.0
new: 32.0

path: .spaces[0].metrics.halstead.level
old: 0.12068965517241378
new: 0.019699812382739212

path: .spaces[0].metrics.halstead.volume
old: 312.7524354002241
new: 23294.894825903815

path: .spaces[0].metrics.halstead.bugs
old: 0.06288771973478276
new: 3.727444518821845

path: .spaces[0].metrics.halstead.n2
old: 21.0
new: 336.0

path: .spaces[0].metrics.halstead.N2
old: 29.0
new: 1066.0

path: .spaces[0].metrics.halstead.effort
old: 2591.3773218875713
new: 1182493.2325911175

path: .spaces[0].metrics.halstead.length
old: 62.0
new: 2733.0

path: .spaces[0].metrics.halstead.N1
old: 33.0
new: 1667.0

path: .spaces[0].metrics.halstead.time
old: 143.96540677153175
new: 65694.06847728431

path: .spaces[0].metrics.halstead.purity_ratio
old: 2.181584127209804
new: 1.0903105210587865

path: .spaces[0].metrics.halstead.difficulty
old: 8.285714285714286
new: 50.76190476190476

path: .spaces[0].metrics.nom.functions
old: 1.0
new: 64.0

path: .spaces[0].metrics.nom.total
old: 1.0
new: 64.0

path: .spaces[0].metrics.mi.mi_visual_studio
old: 54.87709675612262
new: 0.0

path: .spaces[0].metrics.mi.mi_sei
old: 84.57259521298842
new: -74.26661406962015

path: .spaces[0].metrics.mi.mi_original
old: 93.83983545296968
new: -24.572989423357654

path: .spaces[0].metrics.nexits.average
old: 0.0
new: 1.265625

path: .spaces[0].metrics.nexits.sum
old: 0.0
new: 81.0

Code

namespace mozilla {
namespace widget {

class GfxInfo::GLStrings {
  nsCString mVendor;
  nsCString mRenderer;
  nsCString mVersion;
  nsTArray mExtensions;
  bool mReady;

 public:
  GLStrings() : mReady(false) {}

  const nsCString& Vendor() {
    EnsureInitialized();
    return mVendor;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_VENDOR was set.
  void SpoofVendor(const nsCString& s) { mVendor = s; }

  const nsCString& Renderer() {
    EnsureInitialized();
    return mRenderer;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_RENDERER was set.
  void SpoofRenderer(const nsCString& s) { mRenderer = s; }

  const nsCString& Version() {
    EnsureInitialized();
    return mVersion;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_VERSION was set.
  void SpoofVersion(const nsCString& s) { mVersion = s; }

  const nsTArray& Extensions() {
    EnsureInitialized();
    return mExtensions;
  }

  void EnsureInitialized() {
    if (mReady) {
      return;
    }

    RefPtr gl;
    nsCString discardFailureId;
    gl = gl::GLContextProvider::CreateHeadless(
        {gl::CreateContextFlags::REQUIRE_COMPAT_PROFILE}, &discardFailureId);

    if (!gl) {
      // Setting mReady to true here means that we won't retry. Everything will
      // remain blocklisted forever. Ideally, we would like to update that once
      // any GLContext is successfully created, like the compositor's GLContext.
      mReady = true;
      return;
    }

    gl->MakeCurrent();

    if (mVendor.IsEmpty()) {
      const char* spoofedVendor = PR_GetEnv("MOZ_GFX_SPOOF_GL_VENDOR");
      if (spoofedVendor) {
        mVendor.Assign(spoofedVendor);
      } else {
        mVendor.Assign((const char*)gl->fGetString(LOCAL_GL_VENDOR));
      }
    }

    if (mRenderer.IsEmpty()) {
      const char* spoofedRenderer = PR_GetEnv("MOZ_GFX_SPOOF_GL_RENDERER");
      if (spoofedRenderer) {
        mRenderer.Assign(spoofedRenderer);
      } else {
        mRenderer.Assign((const char*)gl->fGetString(LOCAL_GL_RENDERER));
      }
    }

    if (mVersion.IsEmpty()) {
      const char* spoofedVersion = PR_GetEnv("MOZ_GFX_SPOOF_GL_VERSION");
      if (spoofedVersion) {
        mVersion.Assign(spoofedVersion);
      } else {
        mVersion.Assign((const char*)gl->fGetString(LOCAL_GL_VERSION));
      }
    }

    if (mExtensions.IsEmpty()) {
      nsCString rawExtensions;
      rawExtensions.Assign((const char*)gl->fGetString(LOCAL_GL_EXTENSIONS));
      rawExtensions.Trim(" ");

      for (auto extension : rawExtensions.Split(' ')) {
        mExtensions.AppendElement(extension);
      }
    }

    mReady = true;
  }
};

#ifdef DEBUG
NS_IMPL_ISUPPORTS_INHERITED(GfxInfo, GfxInfoBase, nsIGfxInfoDebug)
#endif

GfxInfo::GfxInfo()
    : mInitialized(false),
      mGLStrings(new GLStrings),
      mOSVersionInteger(0),
      mSDKVersion(0) {}

GfxInfo::~GfxInfo() {}

/* GetD2DEnabled and GetDwriteEnabled shouldn't be called until after
 * gfxPlatform initialization has occurred because they depend on it for
 * information. (See bug 591561) */
nsresult GfxInfo::GetD2DEnabled(bool* aEnabled) { return NS_ERROR_FAILURE; }

nsresult GfxInfo::GetDWriteEnabled(bool* aEnabled) { return NS_ERROR_FAILURE; }

nsresult GfxInfo::GetHasBattery(bool* aHasBattery) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetDWriteVersion(nsAString& aDwriteVersion) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetEmbeddedInFirefoxReality(bool* aEmbeddedInFirefoxReality) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetCleartypeParameters(nsAString& aCleartypeParams) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetWindowProtocol(nsAString& aWindowProtocol) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetDesktopEnvironment(nsAString& aDesktopEnvironment) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetTestType(nsAString& aTestType) { return NS_ERROR_NOT_IMPLEMENTED; }

void GfxInfo::EnsureInitialized() {
  if (mInitialized) return;

  if (!mozilla::AndroidBridge::Bridge()) {
    gfxWarning() << "AndroidBridge missing during initialization";
    return;
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField("android/os/Build",
                                                             "MODEL", mModel)) {
    mAdapterDescription.AppendPrintf("Model: %s",
                                     NS_LossyConvertUTF16toASCII(mModel).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
          "android/os/Build", "PRODUCT", mProduct)) {
    mAdapterDescription.AppendPrintf(
        ", Product: %s", NS_LossyConvertUTF16toASCII(mProduct).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
          "android/os/Build", "MANUFACTURER", mManufacturer)) {
    mAdapterDescription.AppendPrintf(
        ", Manufacturer: %s", NS_LossyConvertUTF16toASCII(mManufacturer).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticIntField(
          "android/os/Build$VERSION", "SDK_INT", &mSDKVersion)) {
    // the HARDWARE field isn't available on Android SDK < 8, but we require 9+
    // anyway.
    MOZ_ASSERT(mSDKVersion >= 8);
    if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
            "android/os/Build", "HARDWARE", mHardware)) {
      mAdapterDescription.AppendPrintf(
          ", Hardware: %s", NS_LossyConvertUTF16toASCII(mHardware).get());
    }
  } else {
    mSDKVersion = 0;
  }

  nsString release;
  mozilla::AndroidBridge::Bridge()->GetStaticStringField(
      "android/os/Build$VERSION", "RELEASE", release);
  mOSVersion = NS_LossyConvertUTF16toASCII(release);

  mOSVersionInteger = 0;
  char a[5], b[5], c[5], d[5];
  SplitDriverVersion(mOSVersion.get(), a, b, c, d);
  uint8_t na = atoi(a);
  uint8_t nb = atoi(b);
  uint8_t nc = atoi(c);
  uint8_t nd = atoi(d);

  mOSVersionInteger = (uint32_t(na) << 24) | (uint32_t(nb) << 16) |
                      (uint32_t(nc) << 8) | uint32_t(nd);

  mAdapterDescription.AppendPrintf(
      ", OpenGL: %s -- %s -- %s", mGLStrings->Vendor().get(),
      mGLStrings->Renderer().get(), mGLStrings->Version().get());

  AddCrashReportAnnotations();

  mScreenInfo.mScreenDimensions =
      mozilla::AndroidBridge::Bridge()->getScreenSize();

  mInitialized = true;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDescription(nsAString& aAdapterDescription) {
  EnsureInitialized();
  aAdapterDescription = NS_ConvertASCIItoUTF16(mAdapterDescription);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDescription2(nsAString& aAdapterDescription) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterRAM(uint32_t* aAdapterRAM) {
  EnsureInitialized();
  *aAdapterRAM = 0;
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterRAM2(uint32_t* aAdapterRAM) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriver(nsAString& aAdapterDriver) {
  EnsureInitialized();
  aAdapterDriver.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriver2(nsAString& aAdapterDriver) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVendor(nsAString& aAdapterDriverVendor) {
  EnsureInitialized();
  aAdapterDriverVendor.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVendor2(nsAString& aAdapterDriverVendor) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVersion(nsAString& aAdapterDriverVersion) {
  EnsureInitialized();
  aAdapterDriverVersion = NS_ConvertASCIItoUTF16(mGLStrings->Version());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVersion2(nsAString& aAdapterDriverVersion) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverDate(nsAString& aAdapterDriverDate) {
  EnsureInitialized();
  aAdapterDriverDate.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverDate2(nsAString& aAdapterDriverDate) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterVendorID(nsAString& aAdapterVendorID) {
  EnsureInitialized();
  aAdapterVendorID = NS_ConvertASCIItoUTF16(mGLStrings->Vendor());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterVendorID2(nsAString& aAdapterVendorID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDeviceID(nsAString& aAdapterDeviceID) {
  EnsureInitialized();
  aAdapterDeviceID = NS_ConvertASCIItoUTF16(mGLStrings->Renderer());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDeviceID2(nsAString& aAdapterDeviceID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterSubsysID(nsAString& aAdapterSubsysID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterSubsysID2(nsAString& aAdapterSubsysID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetIsGPU2Active(bool* aIsGPU2Active) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetDisplayInfo(nsTArray& aDisplayInfo) {
  EnsureInitialized();
  nsString displayInfo;
  displayInfo.AppendPrintf("%dx%d",
                           (int32_t)mScreenInfo.mScreenDimensions.width,
                           (int32_t)mScreenInfo.mScreenDimensions.height);
  aDisplayInfo.AppendElement(displayInfo);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDisplayWidth(nsTArray& aDisplayWidth) {
  aDisplayWidth.AppendElement((uint32_t)mScreenInfo.mScreenDimensions.width);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDisplayHeight(nsTArray& aDisplayHeight) {
  aDisplayHeight.AppendElement((uint32_t)mScreenInfo.mScreenDimensions.height);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDrmRenderDevice(nsACString& aDrmRenderDevice) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

void GfxInfo::AddCrashReportAnnotations() {
  CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::AdapterVendorID,
                                     mGLStrings->Vendor());
  CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::AdapterDeviceID,
                                     mGLStrings->Renderer());
  CrashReporter::AnnotateCrashReport(
      CrashReporter::Annotation::AdapterDriverVersion, mGLStrings->Version());
}

const nsTArray& GfxInfo::GetGfxDriverInfo() {
  if (sDriverInfo->IsEmpty()) {
    APPEND_TO_DRIVER_BLOCKLIST2(
        OperatingSystem::Android, DeviceFamily::All,
        nsIGfxInfo::FEATURE_OPENGL_LAYERS, nsIGfxInfo::FEATURE_STATUS_OK,
        DRIVER_COMPARISON_IGNORED, GfxDriverInfo::allDriverVersions,
        "FEATURE_OK_FORCE_OPENGL");
  }

  return *sDriverInfo;
}

nsresult GfxInfo::GetFeatureStatusImpl(
    int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedDriverVersion,
    const nsTArray& aDriverInfo, nsACString& aFailureId,
    OperatingSystem* aOS /* = nullptr */) {
  NS_ENSURE_ARG_POINTER(aStatus);
  aSuggestedDriverVersion.SetIsVoid(true);
  *aStatus = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
  OperatingSystem os = mOS;
  if (aOS) *aOS = os;

  if (sShutdownOccurred) {
    return NS_OK;
  }

  // OpenGL layers are never blocklisted on Android.
  // This early return is so we avoid potentially slow
  // GLStrings initialization on startup when we initialize GL layers.
  if (aFeature == nsIGfxInfo::FEATURE_OPENGL_LAYERS) {
    *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
    return NS_OK;
  }

  EnsureInitialized();

  if (mGLStrings->Vendor().IsEmpty() || mGLStrings->Renderer().IsEmpty()) {
    *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
    return NS_OK;
  }

  // Don't evaluate special cases when evaluating the downloaded blocklist.
  if (aDriverInfo.IsEmpty()) {
    if (aFeature == nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION) {
      if (mSDKVersion < 11) {
        // It's slower than software due to not having a compositing fast path
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
        aFailureId = "FEATURE_FAILURE_CANVAS_2D_SDK";
      } else if (mGLStrings->Renderer().Find("Vivante GC1000") != -1) {
        // Blocklist Vivante GC1000. See bug 1248183.
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILED_CANVAS_2D_HW";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBGL_OPENGL) {
      if (mGLStrings->Renderer().Find("Adreno 200") != -1 ||
          mGLStrings->Renderer().Find("Adreno 205") != -1) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_ADRENO_20x";
        return NS_OK;
      }

      if (mSDKVersion <= 17) {
        if (mGLStrings->Renderer().Find("Adreno (TM) 3") != -1) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_ADRENO_3xx";
        }
        return NS_OK;
      }

      if (mHardware.EqualsLiteral("ville")) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_VILLE";
        return NS_OK;
      }
    }

    if (aFeature == FEATURE_STAGEFRIGHT) {
      NS_LossyConvertUTF16toASCII cManufacturer(mManufacturer);
      NS_LossyConvertUTF16toASCII cModel(mModel);
      NS_LossyConvertUTF16toASCII cHardware(mHardware);

      if (cHardware.EqualsLiteral("antares") ||
          cHardware.EqualsLiteral("harmony") ||
          cHardware.EqualsLiteral("picasso") ||
          cHardware.EqualsLiteral("picasso_e") ||
          cHardware.EqualsLiteral("ventana") ||
          cHardware.EqualsLiteral("rk30board")) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_STAGE_HW";
        return NS_OK;
      }

      if (CompareVersions(mOSVersion.get(), "4.1.0") < 0) {
        // Whitelist:
        //   All Samsung ICS devices, except for:
        //     Samsung SGH-I717 (Bug 845729)
        //     Samsung SGH-I727 (Bug 845729)
        //     Samsung SGH-I757 (Bug 845729)
        //   All Galaxy nexus ICS devices
        //   Sony Xperia Ion (LT28) ICS devices
        bool isWhitelisted =
            cModel.Equals("LT28h", nsCaseInsensitiveCStringComparator) ||
            cManufacturer.Equals("samsung",
                                 nsCaseInsensitiveCStringComparator) ||
            cModel.Equals(
                "galaxy nexus",
                nsCaseInsensitiveCStringComparator);  // some Galaxy Nexus
                                                      // have
                                                      // manufacturer=amazon

        if (cModel.Find("SGH-I717", true) != -1 ||
            cModel.Find("SGH-I727", true) != -1 ||
            cModel.Find("SGH-I757", true) != -1) {
          isWhitelisted = false;
        }

        if (!isWhitelisted) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_1_HW";
          return NS_OK;
        }
      } else if (CompareVersions(mOSVersion.get(), "4.2.0") < 0) {
        // Whitelist:
        //   All JB phones except for those in blocklist below
        // Blocklist:
        //   Samsung devices from bug 812881 and 853522.
        //   Motorola XT890 from bug 882342.
        bool isBlocklisted = cModel.Find("GT-P3100", true) != -1 ||
                             cModel.Find("GT-P3110", true) != -1 ||
                             cModel.Find("GT-P3113", true) != -1 ||
                             cModel.Find("GT-P5100", true) != -1 ||
                             cModel.Find("GT-P5110", true) != -1 ||
                             cModel.Find("GT-P5113", true) != -1 ||
                             cModel.Find("XT890", true) != -1;

        if (isBlocklisted) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_2_HW";
          return NS_OK;
        }
      } else if (CompareVersions(mOSVersion.get(), "4.3.0") < 0) {
        // Blocklist all Sony devices
        if (cManufacturer.Find("Sony", true) != -1) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_3_SONY";
          return NS_OK;
        }
      }
    }

    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_ENCODE) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwVp8EncodeSupported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_ENCODE";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_DECODE) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwVp8DecodeSupported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_DECODE";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_H264) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwH264Supported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_H264";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_VP8_HW_DECODE ||
        aFeature == FEATURE_VP9_HW_DECODE) {
      NS_LossyConvertUTF16toASCII model(mModel);
      bool isBlocked =
          // GIFV crash, see bug 1232911.
          model.Equals("GT-N8013", nsCaseInsensitiveCStringComparator);

      if (isBlocked) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_VPx";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER) {
      bool isUnblocked = false;
      const nsCString& gpu = mGLStrings->Renderer();
      NS_LossyConvertUTF16toASCII model(mModel);

#ifdef NIGHTLY_BUILD
      // On Nightly enable Webrender on all Adreno 4xx GPUs
      isUnblocked |= gpu.Find("Adreno (TM) 4", /*ignoreCase*/ true) >= 0;
#endif
      // Enable Webrender on all Adreno 5xx and 6xx GPUs
      isUnblocked |= gpu.Find("Adreno (TM) 5", /*ignoreCase*/ true) >= 0 ||
                     gpu.Find("Adreno (TM) 6", /*ignoreCase*/ true) >= 0;

      // Enable Webrender on all Mali-Txxx GPUs
      isUnblocked |= gpu.Find("Mali-T", /*ignoreCase*/ true) >= 0;

      // Enable Webrender on all Mali-Gxx GPUs...
      isUnblocked |= gpu.Find("Mali-G", /*ignoreCase*/ true) >= 0 &&
                     // Excluding G31 due to bug 1689947.
                     gpu.Find("Mali-G31", /*ignoreCase*/ true) == kNotFound;

      // Webrender requires the extension GL_OES_EGL_image_external_essl3
      // to render video. Bug 1507074 tracks removing this requirement.
      bool supportsImageExternalEssl3 = mGLStrings->Extensions().Contains(
          "GL_OES_EGL_image_external_essl3"_ns);

      if (!isUnblocked) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_WEBRENDER_BLOCKED_DEVICE";
      } else if (!supportsImageExternalEssl3) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_WEBRENDER_NO_IMAGE_EXTERNAL";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS) {
      // Emulator with SwiftShader is buggy when attempting to clear picture
      // cache textures with a scissor rect set.
      const bool isEmulatorSwiftShader =
          mGLStrings->Renderer().Find(
              "Android Emulator OpenGL ES Translator (Google SwiftShader)") >=
          0;
      if (isEmulatorSwiftShader) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_BUG_1603515";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER_OPTIMIZED_SHADERS) {
      // Optimized shaders result in completely broken rendering in at least one
      // Mali-T6xx device. Disable on all T6xx as a precaution until we know
      // more specifically which devices are affected. See bug 1689064 for
      // details.
      const bool isMaliT6xx =
          mGLStrings->Renderer().Find("Mali-T6", /*ignoreCase*/ true) >= 0;
      if (isMaliT6xx) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_BUG_1689064";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }
  }

  return GfxInfoBase::GetFeatureStatusImpl(
      aFeature, aStatus, aSuggestedDriverVersion, aDriverInfo, aFailureId, &os);
}

static nsCString FeatureCacheOsVerPrefName(int32_t aFeature) {
  nsCString osPrefName;
  osPrefName.AppendASCII("gfxinfo.cache.");
  osPrefName.AppendInt(aFeature);
  osPrefName.AppendASCII(".osver");
  return osPrefName;
}

static nsCString FeatureCacheValuePrefName(int32_t aFeature) {
  nsCString osPrefName;
  osPrefName.AppendASCII("gfxinfo.cache.");
  osPrefName.AppendInt(aFeature);
  osPrefName.AppendASCII(".value");
  return osPrefName;
}

static bool GetCachedFeatureVal(int32_t aFeature, uint32_t aExpectedOsVer,
                                int32_t& aOutStatus) {
  uint32_t osVer = 0;
  nsresult rv =
      Preferences::GetUint(FeatureCacheOsVerPrefName(aFeature).get(), &osVer);
  if (NS_FAILED(rv) || osVer != aExpectedOsVer) {
    return false;
  }
  int32_t status = 0;
  rv = Preferences::GetInt(FeatureCacheValuePrefName(aFeature).get(), &status);
  if (NS_FAILED(rv)) {
    return false;
  }
  aOutStatus = status;
  return true;
}

static void SetCachedFeatureVal(int32_t aFeature, uint32_t aOsVer,
                                int32_t aStatus) {
  // Ignore failures; not much we can do anyway.
  Preferences::SetUint(FeatureCacheOsVerPrefName(aFeature).get(), aOsVer);
  Preferences::SetInt(FeatureCacheValuePrefName(aFeature).get(), aStatus);
}

int32_t GfxInfo::WebRtcHwVp8EncodeSupported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this calculation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_ENCODE,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWVP8Encoder()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_ENCODE, mOSVersionInteger,
                      status);

  return status;
}

int32_t GfxInfo::WebRtcHwVp8DecodeSupported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this caclulation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_DECODE,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWVP8Decoder()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_DECODE, mOSVersionInteger,
                      status);

  return status;
}

int32_t GfxInfo::WebRtcHwH264Supported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this calculation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_H264,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWH264()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_H264, mOSVersionInteger,
                      status);

  return status;
}

#ifdef DEBUG

// Implement nsIGfxInfoDebug

NS_IMETHODIMP GfxInfo::SpoofVendorID(const nsAString& aVendorID) {
  mGLStrings->SpoofVendor(NS_LossyConvertUTF16toASCII(aVendorID));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofDeviceID(const nsAString& aDeviceID) {
  mGLStrings->SpoofRenderer(NS_LossyConvertUTF16toASCII(aDeviceID));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofDriverVersion(const nsAString& aDriverVersion) {
  mGLStrings->SpoofVersion(NS_LossyConvertUTF16toASCII(aDriverVersion));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofOSVersion(uint32_t aVersion) {
  EnsureInitialized();
  mOSVersion = aVersion;
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::FireTestProcess() { return NS_OK; }

#endif

nsString GfxInfo::Model() {
  EnsureInitialized();
  return mModel;
}

nsString GfxInfo::Hardware() {
  EnsureInitialized();
  return mHardware;
}

nsString GfxInfo::Product() {
  EnsureInitialized();
  return mProduct;
}

nsString GfxInfo::Manufacturer() {
  EnsureInitialized();
  return mManufacturer;
}

uint32_t GfxInfo::OperatingSystemVersion() {
  EnsureInitialized();
  return mOSVersionInteger;
}

}  // namespace widget
}  // namespace mozilla

Minimal test - lines (22, 832)

path: .spaces[0].spaces[0].metrics.nom.functions
old: 1.0
new: 64.0

path: .spaces[0].spaces[0].metrics.nom.total
old: 1.0
new: 64.0

path: .spaces[0].spaces[0].metrics.nexits.sum
old: 0.0
new: 81.0

path: .spaces[0].spaces[0].metrics.nexits.average
old: 0.0
new: 1.265625

path: .spaces[0].spaces[0].metrics.cyclomatic.sum
old: 1.0
new: 150.0

path: .spaces[0].spaces[0].metrics.cyclomatic.average
old: 1.0
new: 2.272727272727273

path: .spaces[0].spaces[0].metrics.loc.lloc
old: 3.0
new: 293.0

path: .spaces[0].spaces[0].metrics.loc.blank
old: 2.0
new: 123.0

path: .spaces[0].spaces[0].metrics.loc.sloc
old: 15.0
new: 811.0

path: .spaces[0].spaces[0].metrics.loc.ploc
old: 11.0
new: 622.0

path: .spaces[0].spaces[0].metrics.loc.cloc
old: 2.0
new: 66.0

path: .spaces[0].spaces[0].metrics.halstead.estimated_program_length
old: 113.92990370430176
new: 2969.9857905906206

path: .spaces[0].spaces[0].metrics.halstead.purity_ratio
old: 2.071452794623668
new: 1.0875085282279826

path: .spaces[0].spaces[0].metrics.halstead.volume
old: 267.18895473201644
new: 23267.126606514816

path: .spaces[0].spaces[0].metrics.halstead.effort
old: 1898.4478362538011
new: 1183498.0220149625

path: .spaces[0].spaces[0].metrics.halstead.time
old: 105.46932423632228
new: 65749.89011194237

path: .spaces[0].spaces[0].metrics.halstead.N1
old: 28.0
new: 1666.0

path: .spaces[0].spaces[0].metrics.halstead.N2
old: 27.0
new: 1065.0

path: .spaces[0].spaces[0].metrics.halstead.difficulty
old: 7.105263157894737
new: 50.865671641791046

path: .spaces[0].spaces[0].metrics.halstead.level
old: 0.14074074074074072
new: 0.01965962441314554

path: .spaces[0].spaces[0].metrics.halstead.n2
old: 19.0
new: 335.0

path: .spaces[0].spaces[0].metrics.halstead.bugs
old: 0.05110670217663938
new: 3.729555745348911

path: .spaces[0].spaces[0].metrics.halstead.vocabulary
old: 29.0
new: 367.0

path: .spaces[0].spaces[0].metrics.halstead.length
old: 55.0
new: 2731.0

path: .spaces[0].spaces[0].metrics.halstead.n1
old: 10.0
new: 32.0

path: .spaces[0].spaces[0].metrics.mi.mi_sei
old: 92.35717585613271
new: -74.09599843722144

path: .spaces[0].spaces[0].metrics.mi.mi_original
old: 97.84221500276216
new: -24.296885670661155

path: .spaces[0].spaces[0].metrics.mi.mi_visual_studio
old: 57.21766959225858
new: 0.0

path: .spaces[0].spaces[0].metrics.nargs.sum
old: 0.0
new: 54.0

path: .spaces[0].spaces[0].metrics.nargs.average
old: 0.0
new: 0.84375

path: .spaces[0].spaces[0].metrics.cognitive.sum
old: 0.0
new: 138.0

path: .spaces[0].spaces[0].metrics.cognitive.average
old: 0.0
new: 2.15625

Code

namespace widget {

class GfxInfo::GLStrings {
  nsCString mVendor;
  nsCString mRenderer;
  nsCString mVersion;
  nsTArray mExtensions;
  bool mReady;

 public:
  GLStrings() : mReady(false) {}

  const nsCString& Vendor() {
    EnsureInitialized();
    return mVendor;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_VENDOR was set.
  void SpoofVendor(const nsCString& s) { mVendor = s; }

  const nsCString& Renderer() {
    EnsureInitialized();
    return mRenderer;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_RENDERER was set.
  void SpoofRenderer(const nsCString& s) { mRenderer = s; }

  const nsCString& Version() {
    EnsureInitialized();
    return mVersion;
  }

  // This spoofed value wins, even if the environment variable
  // MOZ_GFX_SPOOF_GL_VERSION was set.
  void SpoofVersion(const nsCString& s) { mVersion = s; }

  const nsTArray& Extensions() {
    EnsureInitialized();
    return mExtensions;
  }

  void EnsureInitialized() {
    if (mReady) {
      return;
    }

    RefPtr gl;
    nsCString discardFailureId;
    gl = gl::GLContextProvider::CreateHeadless(
        {gl::CreateContextFlags::REQUIRE_COMPAT_PROFILE}, &discardFailureId);

    if (!gl) {
      // Setting mReady to true here means that we won't retry. Everything will
      // remain blocklisted forever. Ideally, we would like to update that once
      // any GLContext is successfully created, like the compositor's GLContext.
      mReady = true;
      return;
    }

    gl->MakeCurrent();

    if (mVendor.IsEmpty()) {
      const char* spoofedVendor = PR_GetEnv("MOZ_GFX_SPOOF_GL_VENDOR");
      if (spoofedVendor) {
        mVendor.Assign(spoofedVendor);
      } else {
        mVendor.Assign((const char*)gl->fGetString(LOCAL_GL_VENDOR));
      }
    }

    if (mRenderer.IsEmpty()) {
      const char* spoofedRenderer = PR_GetEnv("MOZ_GFX_SPOOF_GL_RENDERER");
      if (spoofedRenderer) {
        mRenderer.Assign(spoofedRenderer);
      } else {
        mRenderer.Assign((const char*)gl->fGetString(LOCAL_GL_RENDERER));
      }
    }

    if (mVersion.IsEmpty()) {
      const char* spoofedVersion = PR_GetEnv("MOZ_GFX_SPOOF_GL_VERSION");
      if (spoofedVersion) {
        mVersion.Assign(spoofedVersion);
      } else {
        mVersion.Assign((const char*)gl->fGetString(LOCAL_GL_VERSION));
      }
    }

    if (mExtensions.IsEmpty()) {
      nsCString rawExtensions;
      rawExtensions.Assign((const char*)gl->fGetString(LOCAL_GL_EXTENSIONS));
      rawExtensions.Trim(" ");

      for (auto extension : rawExtensions.Split(' ')) {
        mExtensions.AppendElement(extension);
      }
    }

    mReady = true;
  }
};

#ifdef DEBUG
NS_IMPL_ISUPPORTS_INHERITED(GfxInfo, GfxInfoBase, nsIGfxInfoDebug)
#endif

GfxInfo::GfxInfo()
    : mInitialized(false),
      mGLStrings(new GLStrings),
      mOSVersionInteger(0),
      mSDKVersion(0) {}

GfxInfo::~GfxInfo() {}

/* GetD2DEnabled and GetDwriteEnabled shouldn't be called until after
 * gfxPlatform initialization has occurred because they depend on it for
 * information. (See bug 591561) */
nsresult GfxInfo::GetD2DEnabled(bool* aEnabled) { return NS_ERROR_FAILURE; }

nsresult GfxInfo::GetDWriteEnabled(bool* aEnabled) { return NS_ERROR_FAILURE; }

nsresult GfxInfo::GetHasBattery(bool* aHasBattery) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetDWriteVersion(nsAString& aDwriteVersion) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetEmbeddedInFirefoxReality(bool* aEmbeddedInFirefoxReality) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetCleartypeParameters(nsAString& aCleartypeParams) {
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetWindowProtocol(nsAString& aWindowProtocol) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetDesktopEnvironment(nsAString& aDesktopEnvironment) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP
GfxInfo::GetTestType(nsAString& aTestType) { return NS_ERROR_NOT_IMPLEMENTED; }

void GfxInfo::EnsureInitialized() {
  if (mInitialized) return;

  if (!mozilla::AndroidBridge::Bridge()) {
    gfxWarning() << "AndroidBridge missing during initialization";
    return;
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField("android/os/Build",
                                                             "MODEL", mModel)) {
    mAdapterDescription.AppendPrintf("Model: %s",
                                     NS_LossyConvertUTF16toASCII(mModel).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
          "android/os/Build", "PRODUCT", mProduct)) {
    mAdapterDescription.AppendPrintf(
        ", Product: %s", NS_LossyConvertUTF16toASCII(mProduct).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
          "android/os/Build", "MANUFACTURER", mManufacturer)) {
    mAdapterDescription.AppendPrintf(
        ", Manufacturer: %s", NS_LossyConvertUTF16toASCII(mManufacturer).get());
  }

  if (mozilla::AndroidBridge::Bridge()->GetStaticIntField(
          "android/os/Build$VERSION", "SDK_INT", &mSDKVersion)) {
    // the HARDWARE field isn't available on Android SDK < 8, but we require 9+
    // anyway.
    MOZ_ASSERT(mSDKVersion >= 8);
    if (mozilla::AndroidBridge::Bridge()->GetStaticStringField(
            "android/os/Build", "HARDWARE", mHardware)) {
      mAdapterDescription.AppendPrintf(
          ", Hardware: %s", NS_LossyConvertUTF16toASCII(mHardware).get());
    }
  } else {
    mSDKVersion = 0;
  }

  nsString release;
  mozilla::AndroidBridge::Bridge()->GetStaticStringField(
      "android/os/Build$VERSION", "RELEASE", release);
  mOSVersion = NS_LossyConvertUTF16toASCII(release);

  mOSVersionInteger = 0;
  char a[5], b[5], c[5], d[5];
  SplitDriverVersion(mOSVersion.get(), a, b, c, d);
  uint8_t na = atoi(a);
  uint8_t nb = atoi(b);
  uint8_t nc = atoi(c);
  uint8_t nd = atoi(d);

  mOSVersionInteger = (uint32_t(na) << 24) | (uint32_t(nb) << 16) |
                      (uint32_t(nc) << 8) | uint32_t(nd);

  mAdapterDescription.AppendPrintf(
      ", OpenGL: %s -- %s -- %s", mGLStrings->Vendor().get(),
      mGLStrings->Renderer().get(), mGLStrings->Version().get());

  AddCrashReportAnnotations();

  mScreenInfo.mScreenDimensions =
      mozilla::AndroidBridge::Bridge()->getScreenSize();

  mInitialized = true;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDescription(nsAString& aAdapterDescription) {
  EnsureInitialized();
  aAdapterDescription = NS_ConvertASCIItoUTF16(mAdapterDescription);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDescription2(nsAString& aAdapterDescription) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterRAM(uint32_t* aAdapterRAM) {
  EnsureInitialized();
  *aAdapterRAM = 0;
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterRAM2(uint32_t* aAdapterRAM) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriver(nsAString& aAdapterDriver) {
  EnsureInitialized();
  aAdapterDriver.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriver2(nsAString& aAdapterDriver) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVendor(nsAString& aAdapterDriverVendor) {
  EnsureInitialized();
  aAdapterDriverVendor.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVendor2(nsAString& aAdapterDriverVendor) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVersion(nsAString& aAdapterDriverVersion) {
  EnsureInitialized();
  aAdapterDriverVersion = NS_ConvertASCIItoUTF16(mGLStrings->Version());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverVersion2(nsAString& aAdapterDriverVersion) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverDate(nsAString& aAdapterDriverDate) {
  EnsureInitialized();
  aAdapterDriverDate.Truncate();
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDriverDate2(nsAString& aAdapterDriverDate) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterVendorID(nsAString& aAdapterVendorID) {
  EnsureInitialized();
  aAdapterVendorID = NS_ConvertASCIItoUTF16(mGLStrings->Vendor());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterVendorID2(nsAString& aAdapterVendorID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDeviceID(nsAString& aAdapterDeviceID) {
  EnsureInitialized();
  aAdapterDeviceID = NS_ConvertASCIItoUTF16(mGLStrings->Renderer());
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetAdapterDeviceID2(nsAString& aAdapterDeviceID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterSubsysID(nsAString& aAdapterSubsysID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetAdapterSubsysID2(nsAString& aAdapterSubsysID) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetIsGPU2Active(bool* aIsGPU2Active) {
  EnsureInitialized();
  return NS_ERROR_FAILURE;
}

NS_IMETHODIMP
GfxInfo::GetDisplayInfo(nsTArray& aDisplayInfo) {
  EnsureInitialized();
  nsString displayInfo;
  displayInfo.AppendPrintf("%dx%d",
                           (int32_t)mScreenInfo.mScreenDimensions.width,
                           (int32_t)mScreenInfo.mScreenDimensions.height);
  aDisplayInfo.AppendElement(displayInfo);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDisplayWidth(nsTArray& aDisplayWidth) {
  aDisplayWidth.AppendElement((uint32_t)mScreenInfo.mScreenDimensions.width);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDisplayHeight(nsTArray& aDisplayHeight) {
  aDisplayHeight.AppendElement((uint32_t)mScreenInfo.mScreenDimensions.height);
  return NS_OK;
}

NS_IMETHODIMP
GfxInfo::GetDrmRenderDevice(nsACString& aDrmRenderDevice) {
  return NS_ERROR_NOT_IMPLEMENTED;
}

void GfxInfo::AddCrashReportAnnotations() {
  CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::AdapterVendorID,
                                     mGLStrings->Vendor());
  CrashReporter::AnnotateCrashReport(CrashReporter::Annotation::AdapterDeviceID,
                                     mGLStrings->Renderer());
  CrashReporter::AnnotateCrashReport(
      CrashReporter::Annotation::AdapterDriverVersion, mGLStrings->Version());
}

const nsTArray& GfxInfo::GetGfxDriverInfo() {
  if (sDriverInfo->IsEmpty()) {
    APPEND_TO_DRIVER_BLOCKLIST2(
        OperatingSystem::Android, DeviceFamily::All,
        nsIGfxInfo::FEATURE_OPENGL_LAYERS, nsIGfxInfo::FEATURE_STATUS_OK,
        DRIVER_COMPARISON_IGNORED, GfxDriverInfo::allDriverVersions,
        "FEATURE_OK_FORCE_OPENGL");
  }

  return *sDriverInfo;
}

nsresult GfxInfo::GetFeatureStatusImpl(
    int32_t aFeature, int32_t* aStatus, nsAString& aSuggestedDriverVersion,
    const nsTArray& aDriverInfo, nsACString& aFailureId,
    OperatingSystem* aOS /* = nullptr */) {
  NS_ENSURE_ARG_POINTER(aStatus);
  aSuggestedDriverVersion.SetIsVoid(true);
  *aStatus = nsIGfxInfo::FEATURE_STATUS_UNKNOWN;
  OperatingSystem os = mOS;
  if (aOS) *aOS = os;

  if (sShutdownOccurred) {
    return NS_OK;
  }

  // OpenGL layers are never blocklisted on Android.
  // This early return is so we avoid potentially slow
  // GLStrings initialization on startup when we initialize GL layers.
  if (aFeature == nsIGfxInfo::FEATURE_OPENGL_LAYERS) {
    *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
    return NS_OK;
  }

  EnsureInitialized();

  if (mGLStrings->Vendor().IsEmpty() || mGLStrings->Renderer().IsEmpty()) {
    *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
    return NS_OK;
  }

  // Don't evaluate special cases when evaluating the downloaded blocklist.
  if (aDriverInfo.IsEmpty()) {
    if (aFeature == nsIGfxInfo::FEATURE_CANVAS2D_ACCELERATION) {
      if (mSDKVersion < 11) {
        // It's slower than software due to not having a compositing fast path
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_OS_VERSION;
        aFailureId = "FEATURE_FAILURE_CANVAS_2D_SDK";
      } else if (mGLStrings->Renderer().Find("Vivante GC1000") != -1) {
        // Blocklist Vivante GC1000. See bug 1248183.
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILED_CANVAS_2D_HW";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBGL_OPENGL) {
      if (mGLStrings->Renderer().Find("Adreno 200") != -1 ||
          mGLStrings->Renderer().Find("Adreno 205") != -1) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_ADRENO_20x";
        return NS_OK;
      }

      if (mSDKVersion <= 17) {
        if (mGLStrings->Renderer().Find("Adreno (TM) 3") != -1) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_ADRENO_3xx";
        }
        return NS_OK;
      }

      if (mHardware.EqualsLiteral("ville")) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_VILLE";
        return NS_OK;
      }
    }

    if (aFeature == FEATURE_STAGEFRIGHT) {
      NS_LossyConvertUTF16toASCII cManufacturer(mManufacturer);
      NS_LossyConvertUTF16toASCII cModel(mModel);
      NS_LossyConvertUTF16toASCII cHardware(mHardware);

      if (cHardware.EqualsLiteral("antares") ||
          cHardware.EqualsLiteral("harmony") ||
          cHardware.EqualsLiteral("picasso") ||
          cHardware.EqualsLiteral("picasso_e") ||
          cHardware.EqualsLiteral("ventana") ||
          cHardware.EqualsLiteral("rk30board")) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_STAGE_HW";
        return NS_OK;
      }

      if (CompareVersions(mOSVersion.get(), "4.1.0") < 0) {
        // Whitelist:
        //   All Samsung ICS devices, except for:
        //     Samsung SGH-I717 (Bug 845729)
        //     Samsung SGH-I727 (Bug 845729)
        //     Samsung SGH-I757 (Bug 845729)
        //   All Galaxy nexus ICS devices
        //   Sony Xperia Ion (LT28) ICS devices
        bool isWhitelisted =
            cModel.Equals("LT28h", nsCaseInsensitiveCStringComparator) ||
            cManufacturer.Equals("samsung",
                                 nsCaseInsensitiveCStringComparator) ||
            cModel.Equals(
                "galaxy nexus",
                nsCaseInsensitiveCStringComparator);  // some Galaxy Nexus
                                                      // have
                                                      // manufacturer=amazon

        if (cModel.Find("SGH-I717", true) != -1 ||
            cModel.Find("SGH-I727", true) != -1 ||
            cModel.Find("SGH-I757", true) != -1) {
          isWhitelisted = false;
        }

        if (!isWhitelisted) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_1_HW";
          return NS_OK;
        }
      } else if (CompareVersions(mOSVersion.get(), "4.2.0") < 0) {
        // Whitelist:
        //   All JB phones except for those in blocklist below
        // Blocklist:
        //   Samsung devices from bug 812881 and 853522.
        //   Motorola XT890 from bug 882342.
        bool isBlocklisted = cModel.Find("GT-P3100", true) != -1 ||
                             cModel.Find("GT-P3110", true) != -1 ||
                             cModel.Find("GT-P3113", true) != -1 ||
                             cModel.Find("GT-P5100", true) != -1 ||
                             cModel.Find("GT-P5110", true) != -1 ||
                             cModel.Find("GT-P5113", true) != -1 ||
                             cModel.Find("XT890", true) != -1;

        if (isBlocklisted) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_2_HW";
          return NS_OK;
        }
      } else if (CompareVersions(mOSVersion.get(), "4.3.0") < 0) {
        // Blocklist all Sony devices
        if (cManufacturer.Find("Sony", true) != -1) {
          *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
          aFailureId = "FEATURE_FAILURE_4_3_SONY";
          return NS_OK;
        }
      }
    }

    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_ENCODE) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwVp8EncodeSupported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_ENCODE";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_DECODE) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwVp8DecodeSupported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_DECODE";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_WEBRTC_HW_ACCELERATION_H264) {
      if (mozilla::AndroidBridge::Bridge()) {
        *aStatus = WebRtcHwH264Supported();
        aFailureId = "FEATURE_FAILURE_WEBRTC_H264";
        return NS_OK;
      }
    }
    if (aFeature == FEATURE_VP8_HW_DECODE ||
        aFeature == FEATURE_VP9_HW_DECODE) {
      NS_LossyConvertUTF16toASCII model(mModel);
      bool isBlocked =
          // GIFV crash, see bug 1232911.
          model.Equals("GT-N8013", nsCaseInsensitiveCStringComparator);

      if (isBlocked) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_VPx";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER) {
      bool isUnblocked = false;
      const nsCString& gpu = mGLStrings->Renderer();
      NS_LossyConvertUTF16toASCII model(mModel);

#ifdef NIGHTLY_BUILD
      // On Nightly enable Webrender on all Adreno 4xx GPUs
      isUnblocked |= gpu.Find("Adreno (TM) 4", /*ignoreCase*/ true) >= 0;
#endif
      // Enable Webrender on all Adreno 5xx and 6xx GPUs
      isUnblocked |= gpu.Find("Adreno (TM) 5", /*ignoreCase*/ true) >= 0 ||
                     gpu.Find("Adreno (TM) 6", /*ignoreCase*/ true) >= 0;

      // Enable Webrender on all Mali-Txxx GPUs
      isUnblocked |= gpu.Find("Mali-T", /*ignoreCase*/ true) >= 0;

      // Enable Webrender on all Mali-Gxx GPUs...
      isUnblocked |= gpu.Find("Mali-G", /*ignoreCase*/ true) >= 0 &&
                     // Excluding G31 due to bug 1689947.
                     gpu.Find("Mali-G31", /*ignoreCase*/ true) == kNotFound;

      // Webrender requires the extension GL_OES_EGL_image_external_essl3
      // to render video. Bug 1507074 tracks removing this requirement.
      bool supportsImageExternalEssl3 = mGLStrings->Extensions().Contains(
          "GL_OES_EGL_image_external_essl3"_ns);

      if (!isUnblocked) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_WEBRENDER_BLOCKED_DEVICE";
      } else if (!supportsImageExternalEssl3) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_WEBRENDER_NO_IMAGE_EXTERNAL";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_ALLOW_QUALIFIED;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER_SCISSORED_CACHE_CLEARS) {
      // Emulator with SwiftShader is buggy when attempting to clear picture
      // cache textures with a scissor rect set.
      const bool isEmulatorSwiftShader =
          mGLStrings->Renderer().Find(
              "Android Emulator OpenGL ES Translator (Google SwiftShader)") >=
          0;
      if (isEmulatorSwiftShader) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_BUG_1603515";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }

    if (aFeature == FEATURE_WEBRENDER_OPTIMIZED_SHADERS) {
      // Optimized shaders result in completely broken rendering in at least one
      // Mali-T6xx device. Disable on all T6xx as a precaution until we know
      // more specifically which devices are affected. See bug 1689064 for
      // details.
      const bool isMaliT6xx =
          mGLStrings->Renderer().Find("Mali-T6", /*ignoreCase*/ true) >= 0;
      if (isMaliT6xx) {
        *aStatus = nsIGfxInfo::FEATURE_BLOCKED_DEVICE;
        aFailureId = "FEATURE_FAILURE_BUG_1689064";
      } else {
        *aStatus = nsIGfxInfo::FEATURE_STATUS_OK;
      }
      return NS_OK;
    }
  }

  return GfxInfoBase::GetFeatureStatusImpl(
      aFeature, aStatus, aSuggestedDriverVersion, aDriverInfo, aFailureId, &os);
}

static nsCString FeatureCacheOsVerPrefName(int32_t aFeature) {
  nsCString osPrefName;
  osPrefName.AppendASCII("gfxinfo.cache.");
  osPrefName.AppendInt(aFeature);
  osPrefName.AppendASCII(".osver");
  return osPrefName;
}

static nsCString FeatureCacheValuePrefName(int32_t aFeature) {
  nsCString osPrefName;
  osPrefName.AppendASCII("gfxinfo.cache.");
  osPrefName.AppendInt(aFeature);
  osPrefName.AppendASCII(".value");
  return osPrefName;
}

static bool GetCachedFeatureVal(int32_t aFeature, uint32_t aExpectedOsVer,
                                int32_t& aOutStatus) {
  uint32_t osVer = 0;
  nsresult rv =
      Preferences::GetUint(FeatureCacheOsVerPrefName(aFeature).get(), &osVer);
  if (NS_FAILED(rv) || osVer != aExpectedOsVer) {
    return false;
  }
  int32_t status = 0;
  rv = Preferences::GetInt(FeatureCacheValuePrefName(aFeature).get(), &status);
  if (NS_FAILED(rv)) {
    return false;
  }
  aOutStatus = status;
  return true;
}

static void SetCachedFeatureVal(int32_t aFeature, uint32_t aOsVer,
                                int32_t aStatus) {
  // Ignore failures; not much we can do anyway.
  Preferences::SetUint(FeatureCacheOsVerPrefName(aFeature).get(), aOsVer);
  Preferences::SetInt(FeatureCacheValuePrefName(aFeature).get(), aStatus);
}

int32_t GfxInfo::WebRtcHwVp8EncodeSupported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this calculation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_ENCODE,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWVP8Encoder()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_ENCODE, mOSVersionInteger,
                      status);

  return status;
}

int32_t GfxInfo::WebRtcHwVp8DecodeSupported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this caclulation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_DECODE,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWVP8Decoder()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_DECODE, mOSVersionInteger,
                      status);

  return status;
}

int32_t GfxInfo::WebRtcHwH264Supported() {
  MOZ_ASSERT(mozilla::AndroidBridge::Bridge());

  // The Android side of this calculation is very slow, so we cache the result
  // in preferences, invalidating if the OS version changes.

  int32_t status = 0;
  if (GetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_H264,
                          mOSVersionInteger, status)) {
    return status;
  }

  status = mozilla::AndroidBridge::Bridge()->HasHWH264()
               ? nsIGfxInfo::FEATURE_STATUS_OK
               : nsIGfxInfo::FEATURE_BLOCKED_DEVICE;

  SetCachedFeatureVal(FEATURE_WEBRTC_HW_ACCELERATION_H264, mOSVersionInteger,
                      status);

  return status;
}

#ifdef DEBUG

// Implement nsIGfxInfoDebug

NS_IMETHODIMP GfxInfo::SpoofVendorID(const nsAString& aVendorID) {
  mGLStrings->SpoofVendor(NS_LossyConvertUTF16toASCII(aVendorID));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofDeviceID(const nsAString& aDeviceID) {
  mGLStrings->SpoofRenderer(NS_LossyConvertUTF16toASCII(aDeviceID));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofDriverVersion(const nsAString& aDriverVersion) {
  mGLStrings->SpoofVersion(NS_LossyConvertUTF16toASCII(aDriverVersion));
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::SpoofOSVersion(uint32_t aVersion) {
  EnsureInitialized();
  mOSVersion = aVersion;
  return NS_OK;
}

NS_IMETHODIMP GfxInfo::FireTestProcess() { return NS_OK; }

#endif

nsString GfxInfo::Model() {
  EnsureInitialized();
  return mModel;
}

nsString GfxInfo::Hardware() {
  EnsureInitialized();
  return mHardware;
}

nsString GfxInfo::Product() {
  EnsureInitialized();
  return mProduct;
}

nsString GfxInfo::Manufacturer() {
  EnsureInitialized();
  return mManufacturer;
}

uint32_t GfxInfo::OperatingSystemVersion() {
  EnsureInitialized();
  return mOSVersionInteger;
}

}  // namespace widget