summaryrefslogtreecommitdiff
path: root/chromium/third_party/blink/renderer/platform/graphics/canvas_resource_params.cc
blob: 3bec7f3aaf9eb59bec9754fe200f4cb5e0eef31e (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
// Copyright 2020 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_resource_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/blink/renderer/platform/wtf/text/wtf_string.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 {

// The CanvasColorSpace value definitions are specified in the CSS Color Level 4
// specification.
gfx::ColorSpace CanvasColorSpaceToGfxColorSpace(CanvasColorSpace color_space) {
  switch (color_space) {
    case CanvasColorSpace::kSRGB:
      return gfx::ColorSpace::CreateSRGB();
      break;
    case CanvasColorSpace::kRec2020:
      return gfx::ColorSpace(gfx::ColorSpace::PrimaryID::BT2020,
                             gfx::ColorSpace::TransferID::GAMMA24);
      break;
    case CanvasColorSpace::kP3:
      return gfx::ColorSpace::CreateDisplayP3D65();
      break;
  }
  NOTREACHED();
}

}  // namespace

CanvasResourceParams::CanvasResourceParams() = default;

CanvasResourceParams::CanvasResourceParams(CanvasColorSpace color_space,
                                           SkColorType color_type,
                                           SkAlphaType alpha_type)
    : color_space_(color_space),
      color_type_(color_type),
      alpha_type_(alpha_type) {}

CanvasResourceParams::CanvasResourceParams(const SkImageInfo& info)
    : CanvasResourceParams(info.refColorSpace(), info.colorType()) {
  // TODO(https://crbug.com/1157747): This ignores |info|'s SkAlphaType.
}

const SkSurfaceProps* CanvasResourceParams::GetSkSurfaceProps() const {
  static const SkSurfaceProps disable_lcd_props(0, kUnknown_SkPixelGeometry);
  if (alpha_type_ == kOpaque_SkAlphaType)
    return nullptr;
  return &disable_lcd_props;
}

uint8_t CanvasResourceParams::BytesPerPixel() const {
  return SkColorTypeBytesPerPixel(color_type_);
}

gfx::ColorSpace CanvasResourceParams::GetSamplerGfxColorSpace() const {
  // TODO(ccameron): If we add support for uint8srgb as a pixel format, this
  // will need to take into account whether or not this texture will be sampled
  // in linear or nonlinear space.
  return CanvasColorSpaceToGfxColorSpace(color_space_);
}

gfx::ColorSpace CanvasResourceParams::GetStorageGfxColorSpace() const {
  return CanvasColorSpaceToGfxColorSpace(color_space_);
}

sk_sp<SkColorSpace> CanvasResourceParams::GetSkColorSpace() const {
  static_assert(kN32_SkColorType == kRGBA_8888_SkColorType ||
                    kN32_SkColorType == kBGRA_8888_SkColorType,
                "Unexpected kN32_SkColorType value.");
  return CanvasColorSpaceToSkColorSpace(color_space_);
}

gfx::BufferFormat CanvasResourceParams::GetBufferFormat() const {
  switch (color_type_) {
    case kRGBA_F16_SkColorType:
      return gfx::BufferFormat::RGBA_F16;
    case kRGBA_8888_SkColorType:
      return gfx::BufferFormat::RGBA_8888;
    case kBGRA_8888_SkColorType:
      return gfx::BufferFormat::BGRA_8888;
    case kRGB_888x_SkColorType:
      return gfx::BufferFormat::RGBX_8888;
    default:
      NOTREACHED();
  }

  return gfx::BufferFormat::RGBA_8888;
}

GLenum CanvasResourceParams::GLSizedInternalFormat() const {
  switch (color_type_) {
    case kRGBA_F16_SkColorType:
      return GL_RGBA16F;
    case kRGBA_8888_SkColorType:
      return GL_RGBA8;
    case kBGRA_8888_SkColorType:
      return GL_BGRA8_EXT;
    case kRGB_888x_SkColorType:
      return GL_RGB8;
    default:
      NOTREACHED();
  }

  return GL_RGBA8;
}

GLenum CanvasResourceParams::GLType() const {
  switch (color_type_) {
    case kRGBA_F16_SkColorType:
      return GL_HALF_FLOAT_OES;
    case kRGBA_8888_SkColorType:
    case kBGRA_8888_SkColorType:
    case kRGB_888x_SkColorType:
      return GL_UNSIGNED_BYTE;
    default:
      NOTREACHED();
  }

  return GL_UNSIGNED_BYTE;
}

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

CanvasResourceParams::CanvasResourceParams(
    const sk_sp<SkColorSpace> sk_color_space,
    SkColorType sk_color_type) {
  color_space_ = CanvasColorSpaceFromSkColorSpace(sk_color_space.get());
  color_type_ = kN32_SkColorType;
  if (sk_color_type == kRGBA_F16_SkColorType ||
      sk_color_type == kRGBA_8888_SkColorType ||
      sk_color_type == kRGB_888x_SkColorType) {
    color_type_ = sk_color_type;
  }
}

}  // namespace blink