File: bitmap_image_metrics.cc

package info (click to toggle)
chromium 138.0.7204.183-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 6,071,908 kB
  • sloc: cpp: 34,937,088; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,953; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,806; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (121 lines) | stat: -rw-r--r-- 4,511 bytes parent folder | download | duplicates (4)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
// Copyright 2015 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "third_party/blink/renderer/platform/graphics/bitmap_image_metrics.h"

#include "base/metrics/histogram_base.h"
#include "base/metrics/histogram_macros.h"
#include "base/notreached.h"
#include "base/numerics/safe_conversions.h"
#include "media/media_buildflags.h"
#include "third_party/blink/public/common/buildflags.h"
#include "third_party/blink/public/mojom/use_counter/metrics/web_feature.mojom-blink.h"
#include "third_party/blink/renderer/platform/graphics/color_space_gamut.h"
#include "third_party/blink/renderer/platform/instrumentation/histogram.h"
#include "third_party/blink/renderer/platform/instrumentation/use_counter.h"
#include "third_party/blink/renderer/platform/wtf/text/wtf_string.h"
#include "third_party/blink/renderer/platform/wtf/threading.h"
#include "ui/gfx/geometry/size.h"

namespace blink {

BitmapImageMetrics::DecodedImageType
BitmapImageMetrics::StringToDecodedImageType(const String& type) {
  if (type == "jpg")
    return BitmapImageMetrics::DecodedImageType::kJPEG;
  if (type == "png")
    return BitmapImageMetrics::DecodedImageType::kPNG;
  if (type == "gif")
    return BitmapImageMetrics::DecodedImageType::kGIF;
  if (type == "webp")
    return BitmapImageMetrics::DecodedImageType::kWebP;
  if (type == "ico")
    return BitmapImageMetrics::DecodedImageType::kICO;
  if (type == "bmp")
    return BitmapImageMetrics::DecodedImageType::kBMP;
#if BUILDFLAG(ENABLE_AV1_DECODER)
  if (type == "avif")
    return BitmapImageMetrics::DecodedImageType::kAVIF;
#endif
  return BitmapImageMetrics::DecodedImageType::kUnknown;
}

void BitmapImageMetrics::CountDecodedImageType(const String& type) {
  UMA_HISTOGRAM_ENUMERATION("Blink.DecodedImageType",
                            StringToDecodedImageType(type));
}

void BitmapImageMetrics::CountDecodedImageType(const String& type,
                                               UseCounter* use_counter) {
  if (use_counter) {
    if (type == "webp") {
      use_counter->CountUse(WebFeature::kWebPImage);
#if BUILDFLAG(ENABLE_AV1_DECODER)
    } else if (type == "avif") {
      use_counter->CountUse(WebFeature::kAVIFImage);
#endif
    }
  }
}

void BitmapImageMetrics::CountDecodedImageC2PA(UseCounter* use_counter) {
  if (use_counter) {
    use_counter->CountUse(WebFeature::kC2PAManifest);
  }
}

void BitmapImageMetrics::CountDecodedImageDensity(const String& type,
                                                  int image_min_side,
                                                  uint64_t density_centi_bpp,
                                                  size_t image_size_bytes) {
  // All bpp samples are reported in the range 0.01 to 10 bpp as integer number
  // of 0.01 bpp. We don't report for any sample for small images (0 to 99px on
  // the smallest dimension).
  //
  // The histogram JpegDensity.KiBWeighted reports the number of KiB decoded for
  // a given bpp value.
  if (image_min_side < 100)
    return;
  int image_size_kib = static_cast<int>((image_size_bytes + 512) / 1024);
  if (image_size_kib <= 0)
    return;

  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, jpeg_density_histogram,
      ("Blink.DecodedImage.JpegDensity.KiBWeighted", 1, 1000, 100));
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, webp_density_histogram,
      ("Blink.DecodedImage.WebPDensity.KiBWeighted2", 1, 1000, 100));
#if BUILDFLAG(ENABLE_AV1_DECODER)
  DEFINE_THREAD_SAFE_STATIC_LOCAL(
      CustomCountHistogram, avif_density_histogram,
      ("Blink.DecodedImage.AvifDensity.KiBWeighted2", 1, 1000, 100));
#endif

  CustomCountHistogram* density_histogram = nullptr;
  BitmapImageMetrics::DecodedImageType decoded_image_type =
      StringToDecodedImageType(type);
  switch (decoded_image_type) {
    case BitmapImageMetrics::DecodedImageType::kJPEG:
      density_histogram = &jpeg_density_histogram;
      break;
    case BitmapImageMetrics::DecodedImageType::kWebP:
      density_histogram = &webp_density_histogram;
      break;
#if BUILDFLAG(ENABLE_AV1_DECODER)
    case BitmapImageMetrics::DecodedImageType::kAVIF:
      density_histogram = &avif_density_histogram;
      break;
#endif
    default:
      // All other formats are not reported.
      return;
  }

  density_histogram->CountMany(
      base::saturated_cast<base::Histogram::Sample32>(density_centi_bpp),
      image_size_kib);
}

}  // namespace blink