chromium判断是否支持硬件加速的方法

雾里看花,看了许久,最后找到代码了。

https://chromium.googlesource.com/chromium/src/+/master/media/gpu

Android 平台相关的在这里

https://chromium.googlesource.com/chromium/src/+/master/media/gpu/android/
https://chromium.googlesource.com/chromium/src/+/master/media/base/android/media_codec_util.cc
https://chromium.googlesource.com/chromium/src/+/master/media/base/android/java/src/org/chromium/media/MediaCodecUtil.java

举个例子

// static
bool MediaCodecUtil::IsH264EncoderAvailable() {
  return IsMediaCodecAvailable() && IsEncoderSupportedByDevice(kAvcMimeType);
}

...c
// static
bool MediaCodecUtil::IsMediaCodecAvailable() {
  return IsMediaCodecAvailableFor(
      base::android::BuildInfo::GetInstance()->sdk_int(),
      base::android::BuildInfo::GetInstance()->model());
}
// static
bool MediaCodecUtil::IsMediaCodecAvailableFor(int sdk, const char* model) {
  // We will blacklist the model on any sdk that is as old or older than
  // |last_bad_sdk| for the given model.
  struct BlacklistEntry {
    BlacklistEntry(const char* m, int s) : model(m), last_bad_sdk(s) {}
    base::StringPiece model;
    int last_bad_sdk;
    bool operator==(const BlacklistEntry& other) const {
      // Search on name only.  Ignore |last_bad_sdk|.
      return model == other.model;
    }
  };
  static const BlacklistEntry blacklist[] = {
      // crbug.com/653905
      {"LGMS330", SDK_VERSION_LOLLIPOP_MR1},
      // crbug.com/615872
      {"GT-I9100", SDK_VERSION_KITKAT},
      {"GT-I9300", SDK_VERSION_KITKAT},
      {"GT-N7000", SDK_VERSION_KITKAT},
      {"GT-N7100", SDK_VERSION_KITKAT},
      // crbug.com/628509
      {"A6600", SDK_VERSION_KITKAT},
      {"A6800", SDK_VERSION_KITKAT},
      // crbug.com/634920
      {"GT-S7262", SDK_VERSION_KITKAT},
      {"GT-S5282", SDK_VERSION_KITKAT},
      {"GT-I8552", SDK_VERSION_KITKAT},
      // crbug.com/365494, crbug.com/615872
      {"GT-P3113", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-P5110", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-P5100", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-P5113", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-P3110", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-N5110", SDK_VERSION_JELLY_BEAN_MR2},
      {"e-tab4", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-I8200Q", SDK_VERSION_JELLY_BEAN_MR2},
      // crbug.com/693216
      {"GT-I8552B", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-I8262", SDK_VERSION_JELLY_BEAN_MR2},
      {"GT-I8262B", SDK_VERSION_JELLY_BEAN_MR2},
  };
  const BlacklistEntry* iter = std::find(
      std::begin(blacklist), std::end(blacklist), BlacklistEntry(model, 0));
  return iter == std::end(blacklist) || sdk > iter->last_bad_sdk;
}
/**
 * This is a way to blacklist misbehaving devices.
 * @param mime MIME type as passed to mediaCodec.createEncoderByType(mime).
 * @return true if this codec is supported for encoder on this device.
 */
@CalledByNative
static boolean isEncoderSupportedByDevice(String mime) {
    // MediaCodec.setParameters is missing for JB and below, so bitrate
    // can not be adjusted dynamically.
    if (Build.VERSION.SDK_INT < Build.VERSION_CODES.KITKAT) {
        return false;
    }
    // Check if this is supported HW encoder.
    if (mime.equals(MimeTypes.VIDEO_H264)) {
        // Check if device is in H.264 exception list.
        List<String> exceptionModels = Arrays.asList(H264_ENCODER_MODEL_BLACKLIST);
        if (exceptionModels.contains(Build.MODEL)) {
            Log.w(TAG, "Model: " + Build.MODEL + " has blacklisted H.264 encoder.");
            return false;
        }
    }
    return !(findHWEncoder(mime) == null);
}
/**
 * Find HW encoder with given MIME type.
 * @param mime MIME type of the media.
 * @return HWEncoder or null if not found.
 */
private static @Nullable @HWEncoder Integer findHWEncoder(String mime) {
    MediaCodecListHelper codecListHelper = new MediaCodecListHelper();
    for (MediaCodecInfo info : codecListHelper) {
        if (!info.isEncoder() || isSoftwareCodec(info.getName())) continue;
        String encoderName = null;
        for (String mimeType : info.getSupportedTypes()) {
            if (mimeType.equalsIgnoreCase(mime)) {
                encoderName = info.getName();
                break;
            }
        }
        if (encoderName == null) {
            continue; // No HW support in this codec; try the next one.
        }
        // Check if this is supported HW encoder.
        for (@HWEncoder int codecProperties = 0; codecProperties < HWEncoder.NUM_ENTRIES;
                codecProperties++) {
            if (!mime.equalsIgnoreCase(getMimeForHWEncoder(codecProperties))) continue;
            if (encoderName.startsWith(getPrefixForHWEncoder(codecProperties))) {
                if (Build.VERSION.SDK_INT < getMinSDKForHWEncoder(codecProperties)) {
                    Log.w(TAG, "Codec " + encoderName + " is disabled due to SDK version "
                                    + Build.VERSION.SDK_INT);
                    continue;
                }
                Log.d(TAG, "Found target encoder for mime " + mime + " : " + encoderName);
                return codecProperties;
            }
        }
    }
    Log.w(TAG, "HW encoder for " + mime + " is not available on this device.");
    return null;
}

你可能感兴趣的:(Android)