summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/canvas_color_params.cc
blob: 1aedba288aed698fd1b7ac6a4ef1a67fc892f84a (plain)
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
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
// Copyright 2017 The Chromium Authors. All rights reserved.
// 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/canvas_color_params.h"

#include "cc/paint/skia_paint_canvas.h"
#include "components/viz/common/resources/resource_format_utils.h"
#include "third_party/blink/renderer/platform/runtime_enabled_features.h"
#include "third_party/khronos/GLES2/gl2.h"
#include "third_party/khronos/GLES2/gl2ext.h"
#include "third_party/khronos/GLES3/gl3.h"
#include "third_party/skia/include/core/SkSurfaceProps.h"
#include "ui/gfx/color_space.h"

namespace blink {

namespace {

gfx::ColorSpace::PrimaryID GetPrimaryID(CanvasColorSpace color_space) {
  gfx::ColorSpace::PrimaryID primary_id = gfx::ColorSpace::PrimaryID::BT709;
  switch (color_space) {
    case kSRGBCanvasColorSpace:
    case kLinearRGBCanvasColorSpace:
      primary_id = gfx::ColorSpace::PrimaryID::BT709;
      break;
    case kRec2020CanvasColorSpace:
      primary_id = gfx::ColorSpace::PrimaryID::BT2020;
      break;
    case kP3CanvasColorSpace:
      primary_id = gfx::ColorSpace::PrimaryID::SMPTEST432_1;
      break;
  }
  return primary_id;
}

}  // namespace

CanvasColorParams::CanvasColorParams() = default;

CanvasColorParams::CanvasColorParams(CanvasColorSpace color_space,
                                     CanvasPixelFormat pixel_format,
                                     OpacityMode opacity_mode)
    : color_space_(color_space),
      pixel_format_(pixel_format),
      opacity_mode_(opacity_mode) {}

CanvasColorParams::CanvasColorParams(const CanvasColorParams& params,
                                     bool force_rgba)
    : color_space_(params.color_space_),
      pixel_format_(params.pixel_format_),
      opacity_mode_(params.opacity_mode_),
      force_rgba_(force_rgba) {}

CanvasColorParams::CanvasColorParams(const SkImageInfo& info)
    : CanvasColorParams(info.refColorSpace(), info.colorType()) {}

sk_sp<SkColorSpace> CanvasColorParams::GetSkColorSpaceForSkSurfaces() const {
  return GetSkColorSpace();
}

bool CanvasColorParams::NeedsColorConversion(
    const CanvasColorParams& dest_color_params) const {
  if ((color_space_ == dest_color_params.ColorSpace() &&
       pixel_format_ == dest_color_params.PixelFormat()))
    return false;
  return true;
}

SkColorType CanvasColorParams::GetSkColorType() const {
  if (pixel_format_ == kF16CanvasPixelFormat)
    return kRGBA_F16_SkColorType;
  return force_rgba_ ? kRGBA_8888_SkColorType : kN32_SkColorType;
}

SkAlphaType CanvasColorParams::GetSkAlphaType() const {
  return opacity_mode_ == kOpaque ? kOpaque_SkAlphaType : kPremul_SkAlphaType;
}

const SkSurfaceProps* CanvasColorParams::GetSkSurfaceProps() const {
  static const SkSurfaceProps disable_lcd_props(0, kUnknown_SkPixelGeometry);
  if (opacity_mode_ == kOpaque)
    return nullptr;
  return &disable_lcd_props;
}

uint8_t CanvasColorParams::BytesPerPixel() const {
  return SkColorTypeBytesPerPixel(GetSkColorType());
}

gfx::ColorSpace CanvasColorParams::GetSamplerGfxColorSpace() const {
  gfx::ColorSpace::PrimaryID primary_id = GetPrimaryID(color_space_);

  // TODO(ccameron): This needs to take into account whether or not this texture
  // will be sampled in linear or nonlinear space.
  gfx::ColorSpace::TransferID transfer_id =
      gfx::ColorSpace::TransferID::IEC61966_2_1;
  if (pixel_format_ == kF16CanvasPixelFormat)
    transfer_id = gfx::ColorSpace::TransferID::LINEAR_HDR;

  return gfx::ColorSpace(primary_id, transfer_id);
}

gfx::ColorSpace CanvasColorParams::GetStorageGfxColorSpace() const {
  gfx::ColorSpace::PrimaryID primary_id = GetPrimaryID(color_space_);

  gfx::ColorSpace::TransferID transfer_id =
      gfx::ColorSpace::TransferID::IEC61966_2_1;
  // Only sRGB and e-sRGB use sRGB transfer function. Other canvas color spaces,
  // i.e., linear-rgb, p3 and rec2020 use linear transfer function.
  if (color_space_ != kSRGBCanvasColorSpace)
    transfer_id = gfx::ColorSpace::TransferID::LINEAR_HDR;

  return gfx::ColorSpace(primary_id, transfer_id);
}

sk_sp<SkColorSpace> CanvasColorParams::GetSkColorSpace() const {
  skcms_Matrix3x3 gamut = SkNamedGamut::kSRGB;
  skcms_TransferFunction transferFn = SkNamedTransferFn::kSRGB;
  switch (color_space_) {
    case kSRGBCanvasColorSpace:
      break;
    case kLinearRGBCanvasColorSpace:
      transferFn = SkNamedTransferFn::kLinear;
      break;
    case kRec2020CanvasColorSpace:
      gamut = SkNamedGamut::kRec2020;
      transferFn = SkNamedTransferFn::kLinear;
      break;
    case kP3CanvasColorSpace:
      gamut = SkNamedGamut::kDCIP3;
      transferFn = SkNamedTransferFn::kLinear;
      break;
  }
  return SkColorSpace::MakeRGB(transferFn, gamut);
}

gfx::BufferFormat CanvasColorParams::GetBufferFormat() const {
  static_assert(kN32_SkColorType == kRGBA_8888_SkColorType ||
                    kN32_SkColorType == kBGRA_8888_SkColorType,
                "Unexpected kN32_SkColorType value.");

  switch (GetSkColorType()) {
    case kRGBA_8888_SkColorType:
      return gfx::BufferFormat::RGBA_8888;
    case kBGRA_8888_SkColorType:
      return gfx::BufferFormat::BGRA_8888;
    case kRGBA_F16_SkColorType:
      return gfx::BufferFormat::RGBA_F16;
    default:
      NOTREACHED();
  }

  return gfx::BufferFormat::RGBA_8888;
}

GLenum CanvasColorParams::GLUnsizedInternalFormat() const {
  // TODO(junov): try GL_RGB when opacity_mode_ == kOpaque
  static_assert(kN32_SkColorType == kRGBA_8888_SkColorType ||
                    kN32_SkColorType == kBGRA_8888_SkColorType,
                "Unexpected kN32_SkColorType value.");

  switch (GetSkColorType()) {
    case kRGBA_8888_SkColorType:
      return GL_RGBA;
    case kBGRA_8888_SkColorType:
      return GL_BGRA_EXT;
    case kRGBA_F16_SkColorType:
      return GL_RGBA;
    default:
      NOTREACHED();
  }

  return GL_RGBA;
}

GLenum CanvasColorParams::GLSizedInternalFormat() const {
  static_assert(kN32_SkColorType == kRGBA_8888_SkColorType ||
                    kN32_SkColorType == kBGRA_8888_SkColorType,
                "Unexpected kN32_SkColorType value.");

  switch (GetSkColorType()) {
    case kRGBA_8888_SkColorType:
      return GL_RGBA8;
    case kBGRA_8888_SkColorType:
      return GL_BGRA8_EXT;
    case kRGBA_F16_SkColorType:
      return GL_RGBA16F;
    default:
      NOTREACHED();
  }

  return GL_RGBA8;
}

GLenum CanvasColorParams::GLType() const {
  switch (GetSkColorType()) {
    case kRGBA_8888_SkColorType:
    case kBGRA_8888_SkColorType:
      return GL_UNSIGNED_BYTE;
    case kRGBA_F16_SkColorType:
      return GL_HALF_FLOAT_OES;
    default:
      NOTREACHED();
  }

  return GL_UNSIGNED_BYTE;
}

viz::ResourceFormat CanvasColorParams::TransferableResourceFormat() const {
  return viz::GetResourceFormat(GetBufferFormat());
}

CanvasColorParams::CanvasColorParams(const sk_sp<SkColorSpace> color_space,
                                     SkColorType color_type) {
  color_space_ = kSRGBCanvasColorSpace;
  pixel_format_ = kRGBA8CanvasPixelFormat;
  // When there is no color space information, the SkImage is in legacy mode and
  // the color type is kN32_SkColorType (which translates to kRGBA8 canvas pixel
  // format).
  if (!color_space)
    return;

  // kSRGBCanvasColorSpace covers sRGB and e-sRGB. We need to check for
  // linear-rgb, rec2020 and p3.
  if (SkColorSpace::Equals(color_space.get(),
                           SkColorSpace::MakeSRGB()->makeLinearGamma().get())) {
    color_space_ = kLinearRGBCanvasColorSpace;
  } else if (SkColorSpace::Equals(
                 color_space.get(),
                 SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear,
                                       SkNamedGamut::kRec2020)
                     .get())) {
    color_space_ = kRec2020CanvasColorSpace;
  } else if (SkColorSpace::Equals(
                 color_space.get(),
                 SkColorSpace::MakeRGB(SkNamedTransferFn::kLinear,
                                       SkNamedGamut::kDCIP3)
                     .get())) {
    color_space_ = kP3CanvasColorSpace;
  }

  if (color_type == kRGBA_F16_SkColorType)
    pixel_format_ = kF16CanvasPixelFormat;
  else if (color_type == kRGBA_8888_SkColorType)
    force_rgba_ = true;
}

}  // namespace blink