File: image_extractor.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 (155 lines) | stat: -rw-r--r-- 5,599 bytes parent folder | download | duplicates (5)
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
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
// Copyright 2024 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/gpu/image_extractor.h"

#include "third_party/blink/public/platform/platform.h"
#include "third_party/blink/renderer/platform/graphics/skia/skia_utils.h"
#include "third_party/blink/renderer/platform/image-decoders/image_decoder.h"
#include "third_party/skia/include/core/SkColorSpace.h"
#include "third_party/skia/include/core/SkImage.h"

namespace blink {
namespace {
bool FrameIsValid(const SkBitmap& frame_bitmap) {
  if (frame_bitmap.isNull()) {
    return false;
  }
  if (frame_bitmap.empty()) {
    return false;
  }
  if (frame_bitmap.colorType() != kN32_SkColorType &&
      frame_bitmap.colorType() != kRGBA_F16_SkColorType) {
    return false;
  }
  return true;
}
}  // anonymous namespace

ImageExtractor::ImageExtractor(Image* image,
                               SkAlphaType target_alpha_type,
                               sk_sp<SkColorSpace> target_color_space) {
  if (!image) {
    return;
  }

  const auto& paint_image = image->PaintImageForCurrentFrame();
  sk_sp<SkImage> skia_image = paint_image.GetSwSkImage();
  if (skia_image && !skia_image->colorSpace()) {
    skia_image = skia_image->reinterpretColorSpace(SkColorSpace::MakeSRGB());
  }

  if (image->HasData()) {
    bool paint_image_is_f16 =
        paint_image.GetColorType() == kRGBA_F16_SkColorType;

    // If there already exists a decoded image in `skia_image`, determine if we
    // can re-use that image. If we can't, then we need to re-decode the image
    // here.
    bool needs_redecode = false;

    // A BitmapImage indicates that this is a coded backed image but non-decoded
    // yet. Decode the image here.
    if (image->IsBitmapImage()) {
      needs_redecode = true;
    }

    if (skia_image) {
      // The `target_color_space` is set to nullptr iff
      // UNPACK_COLORSPACE_CONVERSION is NONE, which means that the color
      // profile of the image should be ignored. In this case, always re-decode,
      // because we can't reliably know that `skia_image` ignored the image's
      // color profile when it was created.
      if (!target_color_space) {
        needs_redecode = true;
      }

      // If there is a target color space, but the SkImage that was decoded is
      // not already in this color space, then re-decode the image. The reason
      // for this is that repeated color converisons may accumulate clamping and
      // rounding errors.
      if (target_color_space &&
          !SkColorSpace::Equals(skia_image->colorSpace(),
                                target_color_space.get())) {
        needs_redecode = true;
      }

      // If the image was decoded with premultipled alpha and unpremultipled
      // alpha was requested, then re-decode without premultiplying alpha. Don't
      // bother re-decoding if premultiply alpha was requested, because we will
      // do that lossy conversion later.
      if (skia_image->alphaType() == kPremul_SkAlphaType &&
          target_alpha_type != kPremul_SkAlphaType) {
        needs_redecode = true;
      }

      // If the image is high bit depth, but was not decoded as high bit depth,
      // then re-decode the image.
      if (paint_image_is_f16 &&
          skia_image->colorType() != kRGBA_F16_SkColorType) {
        needs_redecode = true;
      }
    } else {
      // If the image has not been decoded yet, then it needs to be decoded.
      needs_redecode = true;
    }

    if (needs_redecode) {
      const bool data_complete = true;

      // Always decode as unpremultiplied. If premultiplication is desired, it
      // will be applied later.
      const auto alpha_option = ImageDecoder::kAlphaNotPremultiplied;

      // Decode to the paint image's bit depth. If conversion is needed, it will
      // be applied later.
      const auto bit_depth = paint_image_is_f16
                                 ? ImageDecoder::kHighBitDepthToHalfFloat
                                 : ImageDecoder::kDefaultBitDepth;

      // If we are not ignoring the color space, then tag the image with the
      // target color space. It will be converted later on.
      const auto color_behavior =
          target_color_space ? ColorBehavior::kTag : ColorBehavior::kIgnore;

      // Decode the image here on the main thread.
      std::unique_ptr<ImageDecoder> decoder(ImageDecoder::Create(
          image->Data(), data_complete, alpha_option, bit_depth, color_behavior,
          cc::AuxImage::kDefault, Platform::GetMaxDecodedImageBytes()));
      if (!decoder || !decoder->FrameCount()) {
        return;
      }
      ImageFrame* frame = decoder->DecodeFrameBufferAtIndex(0);
      if (!frame || frame->GetStatus() != ImageFrame::kFrameComplete) {
        return;
      }
      SkBitmap bitmap = frame->Bitmap();
      if (!FrameIsValid(bitmap)) {
        return;
      }

      // TODO(fmalita): Partial frames are not supported currently: only fully
      // decoded frames make it through.  We could potentially relax this and
      // use SkImages::RasterFromBitmap(bitmap) to make a copy.
      skia_image = frame->FinalizePixelsAndGetImage();
    }
  }

  if (!skia_image) {
    return;
  }

  DCHECK(skia_image->width());
  DCHECK(skia_image->height());

  // Fail if the image was downsampled because of memory limits.
  if (skia_image->width() != image->width() ||
      skia_image->height() != image->height()) {
    return;
  }

  sk_image_ = std::move(skia_image);
}

}  // namespace blink