File: shared_image_format_utils.cc

package info (click to toggle)
chromium 139.0.7258.138-1
  • links: PTS, VCS
  • area: main
  • in suites: sid
  • size: 6,120,676 kB
  • sloc: cpp: 35,100,869; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (307 lines) | stat: -rw-r--r-- 11,807 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
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
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "components/viz/common/resources/shared_image_format_utils.h"

#include <GLES2/gl2.h>
#include <GLES2/gl2ext.h>

#include "base/check_op.h"
#include "base/logging.h"
#include "base/notreached.h"
#include "third_party/skia/include/core/SkColorType.h"
#include "ui/gfx/buffer_types.h"

namespace viz {

SkColorType ToClosestSkColorType(SharedImageFormat format) {
  CHECK(format.is_single_plane());

  if (format == SinglePlaneFormat::kRGBA_4444) {
    return kARGB_4444_SkColorType;
  } else if (format == SinglePlaneFormat::kRGBA_8888) {
    return kRGBA_8888_SkColorType;
  } else if (format == SinglePlaneFormat::kBGRA_8888) {
    return kBGRA_8888_SkColorType;
  } else if (format == SinglePlaneFormat::kALPHA_8) {
    return kAlpha_8_SkColorType;
  } else if (format == SinglePlaneFormat::kBGR_565) {
    return kRGB_565_SkColorType;
  } else if (format == SinglePlaneFormat::kRGBX_8888 ||
             format == SinglePlaneFormat::kBGRX_8888 ||
             format == SinglePlaneFormat::kETC1) {
    return kRGB_888x_SkColorType;
  } else if (format == SinglePlaneFormat::kRGBA_1010102 ||
             // This intentionally returns kRGBA_1010102_SkColorType for
             // BGRA_1010102 even though kBGRA_1010102_SkColorType exists. It
             // should only be used on macOS (outside of tests).
             format == SinglePlaneFormat::kBGRA_1010102) {
    return kRGBA_1010102_SkColorType;

  } else if (format == SinglePlaneFormat::kR_8) {
    return kAlpha_8_SkColorType;
  } else if (format == SinglePlaneFormat::kR_16) {
    return kA16_unorm_SkColorType;
  } else if (format == SinglePlaneFormat::kRG_1616) {
    return kR16G16_unorm_SkColorType;
  } else if (format == SinglePlaneFormat::kLUMINANCE_F16 ||
             format == SinglePlaneFormat::kR_F16) {
    return kA16_float_SkColorType;
  } else if (format == SinglePlaneFormat::kRG_88) {
    return kR8G8_unorm_SkColorType;
  } else if (format == SinglePlaneFormat::kRGBA_F16) {
    return kRGBA_F16_SkColorType;
  }
  NOTREACHED();
}

SkColorType ToClosestSkColorType(SharedImageFormat format, int plane_index) {
  CHECK(format.IsValidPlaneIndex(plane_index));
  if (format.is_single_plane()) {
    return ToClosestSkColorType(format);
  }

  // No external sampling, format is per plane.
  CHECK(!format.PrefersExternalSampler());
  int num_channels = format.NumChannelsInPlane(plane_index);
  DCHECK_GT(num_channels, 0);
  DCHECK_LE(num_channels, 2);
  switch (format.channel_format()) {
    case SharedImageFormat::ChannelFormat::k8:
      return num_channels == 1 ? kAlpha_8_SkColorType : kR8G8_unorm_SkColorType;
    case SharedImageFormat::ChannelFormat::k10:
    case SharedImageFormat::ChannelFormat::k16:
      return num_channels == 1 ? kA16_unorm_SkColorType
                               : kR16G16_unorm_SkColorType;
    case SharedImageFormat::ChannelFormat::k16F:
      return num_channels == 1 ? kA16_float_SkColorType
                               : kR16G16_float_SkColorType;
  }
}

SharedImageFormat SkColorTypeToSinglePlaneSharedImageFormat(
    SkColorType color_type) {
  switch (color_type) {
    case kARGB_4444_SkColorType:
      return SinglePlaneFormat::kRGBA_4444;
    case kBGRA_8888_SkColorType:
      return SinglePlaneFormat::kBGRA_8888;
    case kRGBA_8888_SkColorType:
      return SinglePlaneFormat::kRGBA_8888;
    case kRGBA_F16_SkColorType:
      return SinglePlaneFormat::kRGBA_F16;
    case kAlpha_8_SkColorType:
      return SinglePlaneFormat::kALPHA_8;
    case kRGB_565_SkColorType:
      return SinglePlaneFormat::kBGR_565;
    case kRGB_888x_SkColorType:
      return SinglePlaneFormat::kRGBX_8888;
    case kRGBA_1010102_SkColorType:
      return SinglePlaneFormat::kRGBA_1010102;
    case kBGRA_1010102_SkColorType:
      return SinglePlaneFormat::kBGRA_1010102;
    case kR8G8_unorm_SkColorType:
      return SinglePlaneFormat::kRG_88;
    case kA16_float_SkColorType:
      return SinglePlaneFormat::kR_F16;
    case kA16_unorm_SkColorType:
      return SinglePlaneFormat::kR_16;
    case kR16G16_unorm_SkColorType:
      return SinglePlaneFormat::kRG_1616;
    // These colortypes are just for reading from - not to render to.
    case kR16G16_float_SkColorType:
    case kR16G16B16A16_unorm_SkColorType:
    case kUnknown_SkColorType:
    // These colortypes are don't have an equivalent in SharedImageFormat.
    case kRGB_101010x_SkColorType:
    case kBGR_101010x_SkColorType:
    case kRGBA_F16Norm_SkColorType:
    case kRGBA_F32_SkColorType:
    case kSRGBA_8888_SkColorType:
    // Default case is for new color types added to Skia.
    default:
      break;
  }
  NOTREACHED();
}

bool CanCreateGpuMemoryBufferForSinglePlaneSharedImageFormat(
    SharedImageFormat format) {
  CHECK(format.is_single_plane());
  return (format == SinglePlaneFormat::kBGRA_8888 ||
          format == SinglePlaneFormat::kR_8 ||
          format == SinglePlaneFormat::kRG_88 ||
#if BUILDFLAG(IS_APPLE)
          format == SinglePlaneFormat::kBGRX_8888 ||
          format == SinglePlaneFormat::kRGBX_8888 ||
          format == SinglePlaneFormat::kR_16 ||
          format == SinglePlaneFormat::kRG_1616 ||
#endif
          format == SinglePlaneFormat::kRGBA_4444 ||
          format == SinglePlaneFormat::kRGBA_8888 ||
          format == SinglePlaneFormat::kRGBA_1010102 ||
          format == SinglePlaneFormat::kBGRA_1010102 ||
          format == SinglePlaneFormat::kRGBA_F16);
}

bool HasEquivalentBufferFormat(SharedImageFormat format) {
  return format == SinglePlaneFormat::kBGRA_8888 ||
         format == SinglePlaneFormat::kR_8 ||
         format == SinglePlaneFormat::kR_16 ||
         format == SinglePlaneFormat::kRG_1616 ||
         format == SinglePlaneFormat::kRGBA_4444 ||
         format == SinglePlaneFormat::kRGBA_8888 ||
         format == SinglePlaneFormat::kRGBA_F16 ||
         format == SinglePlaneFormat::kBGR_565 ||
         format == SinglePlaneFormat::kRG_88 ||
         format == SinglePlaneFormat::kRGBX_8888 ||
         format == SinglePlaneFormat::kBGRX_8888 ||
         format == SinglePlaneFormat::kRGBA_1010102 ||
         format == SinglePlaneFormat::kBGRA_1010102 ||
         format == MultiPlaneFormat::kYV12 ||
         format == MultiPlaneFormat::kNV12 ||
         format == MultiPlaneFormat::kNV12A ||
         format == MultiPlaneFormat::kP010;
}

gfx::BufferFormat SinglePlaneSharedImageFormatToBufferFormat(
    SharedImageFormat format) {
  CHECK(format.is_single_plane());
  if (format == SinglePlaneFormat::kBGRA_8888) {
    return gfx::BufferFormat::BGRA_8888;
  } else if (format == SinglePlaneFormat::kR_8) {
    return gfx::BufferFormat::R_8;
  } else if (format == SinglePlaneFormat::kR_16) {
    return gfx::BufferFormat::R_16;
  } else if (format == SinglePlaneFormat::kRG_1616) {
    return gfx::BufferFormat::RG_1616;
  } else if (format == SinglePlaneFormat::kRGBA_4444) {
    return gfx::BufferFormat::RGBA_4444;
  } else if (format == SinglePlaneFormat::kRGBA_8888) {
    return gfx::BufferFormat::RGBA_8888;
  } else if (format == SinglePlaneFormat::kRGBA_F16) {
    return gfx::BufferFormat::RGBA_F16;
  } else if (format == SinglePlaneFormat::kBGR_565) {
    return gfx::BufferFormat::BGR_565;
  } else if (format == SinglePlaneFormat::kRG_88) {
    return gfx::BufferFormat::RG_88;
  } else if (format == SinglePlaneFormat::kRGBX_8888) {
    return gfx::BufferFormat::RGBX_8888;
  } else if (format == SinglePlaneFormat::kBGRX_8888) {
    return gfx::BufferFormat::BGRX_8888;
  } else if (format == SinglePlaneFormat::kRGBA_1010102) {
    return gfx::BufferFormat::RGBA_1010102;
  } else if (format == SinglePlaneFormat::kBGRA_1010102) {
    return gfx::BufferFormat::BGRA_1010102;
  } else {
    // CanCreateGpuMemoryBufferForSinglePlaneSharedImageFormat() returns
    // false for all other types, so give a default value that will not be used.
    return gfx::BufferFormat::RGBA_8888;
  }
}

SharedImageFormat GetSharedImageFormat(gfx::BufferFormat buffer_format) {
  switch (buffer_format) {
    case gfx::BufferFormat::BGRA_8888:
      return SinglePlaneFormat::kBGRA_8888;
    case gfx::BufferFormat::R_8:
      return SinglePlaneFormat::kR_8;
    case gfx::BufferFormat::R_16:
      return SinglePlaneFormat::kR_16;
    case gfx::BufferFormat::RG_1616:
      return SinglePlaneFormat::kRG_1616;
    case gfx::BufferFormat::RGBA_4444:
      return SinglePlaneFormat::kRGBA_4444;
    case gfx::BufferFormat::RGBA_8888:
      return SinglePlaneFormat::kRGBA_8888;
    case gfx::BufferFormat::RGBA_F16:
      return SinglePlaneFormat::kRGBA_F16;
    case gfx::BufferFormat::BGR_565:
      return SinglePlaneFormat::kBGR_565;
    case gfx::BufferFormat::RG_88:
      return SinglePlaneFormat::kRG_88;
    case gfx::BufferFormat::RGBX_8888:
      return SinglePlaneFormat::kRGBX_8888;
    case gfx::BufferFormat::BGRX_8888:
      return SinglePlaneFormat::kBGRX_8888;
    case gfx::BufferFormat::RGBA_1010102:
      return SinglePlaneFormat::kRGBA_1010102;
    case gfx::BufferFormat::BGRA_1010102:
      return SinglePlaneFormat::kBGRA_1010102;
    case gfx::BufferFormat::YVU_420:
      return MultiPlaneFormat::kYV12;
    case gfx::BufferFormat::YUV_420_BIPLANAR:
      return MultiPlaneFormat::kNV12;
    case gfx::BufferFormat::YUVA_420_TRIPLANAR:
      return MultiPlaneFormat::kNV12A;
    case gfx::BufferFormat::P010:
      return MultiPlaneFormat::kP010;
  }
  NOTREACHED();
}

// static
unsigned int
SharedImageFormatRestrictedSinglePlaneUtils::ToGLTextureStorageFormat(
    SharedImageFormat format,
    bool use_angle_rgbx_format) {
  CHECK(format.is_single_plane());
  if (format == SinglePlaneFormat::kRGBA_8888) {
    return GL_RGBA8_OES;
  } else if (format == SinglePlaneFormat::kBGRA_8888) {
    return GL_BGRA8_EXT;
  } else if (format == SinglePlaneFormat::kRGBA_F16) {
    return GL_RGBA16F_EXT;
  } else if (format == SinglePlaneFormat::kRGBA_4444) {
    return GL_RGBA4;
  } else if (format == SinglePlaneFormat::kALPHA_8) {
    return GL_ALPHA8_EXT;
  } else if (format == SinglePlaneFormat::kBGR_565) {
    return GL_RGB565;
  } else if (format == SinglePlaneFormat::kR_8) {
    return GL_R8_EXT;
  } else if (format == SinglePlaneFormat::kRG_88) {
    return GL_RG8_EXT;
  } else if (format == SinglePlaneFormat::kLUMINANCE_F16) {
    return GL_LUMINANCE16F_EXT;
  } else if (format == SinglePlaneFormat::kR_F16) {
    return GL_R16F_EXT;
  } else if (format == SinglePlaneFormat::kR_16) {
    return GL_R16_EXT;
  } else if (format == SinglePlaneFormat::kRG_1616) {
    return GL_RG16_EXT;
  } else if (format == SinglePlaneFormat::kRGBX_8888 ||
             format == SinglePlaneFormat::kBGRX_8888) {
    return use_angle_rgbx_format ? GL_RGBX8_ANGLE : GL_RGB8_OES;
  } else if (format == SinglePlaneFormat::kETC1) {
    return GL_ETC1_RGB8_OES;
  } else if (format == SinglePlaneFormat::kRGBA_1010102 ||
             format == SinglePlaneFormat::kBGRA_1010102) {
    return GL_RGB10_A2_EXT;
  }
  NOTREACHED();
}

// static
gfx::BufferFormat
SharedImageFormatToBufferFormatRestrictedUtils::ToBufferFormat(
    SharedImageFormat format) {
  if (format.is_single_plane()) {
    return SinglePlaneSharedImageFormatToBufferFormat(format);
  }

  if (format == MultiPlaneFormat::kYV12) {
    return gfx::BufferFormat::YVU_420;
  } else if (format == MultiPlaneFormat::kNV12) {
    return gfx::BufferFormat::YUV_420_BIPLANAR;
  } else if (format == MultiPlaneFormat::kNV12A) {
    return gfx::BufferFormat::YUVA_420_TRIPLANAR;
  } else if (format == MultiPlaneFormat::kP010) {
    return gfx::BufferFormat::P010;
  }
  DUMP_WILL_BE_NOTREACHED() << "format=" << format.ToString();
  return gfx::BufferFormat::RGBA_8888;
}

}  // namespace viz