Fix crash on app list start page keyboard navigation with <4 apps.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.cc
blobc16a962f6d59ba61df57af19e1a78b3573ea57f8
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 // This file is here so other GLES2 related files can have a common set of
6 // includes where appropriate.
8 #include <sstream>
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
12 #include <GLES3/gl3.h>
14 #include "base/numerics/safe_math.h"
15 #include "gpu/command_buffer/common/gles2_cmd_format.h"
16 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
18 namespace gpu {
19 namespace gles2 {
21 namespace gl_error_bit {
22 enum GLErrorBit {
23 kNoError = 0,
24 kInvalidEnum = (1 << 0),
25 kInvalidValue = (1 << 1),
26 kInvalidOperation = (1 << 2),
27 kOutOfMemory = (1 << 3),
28 kInvalidFrameBufferOperation = (1 << 4),
29 kContextLost = (1 << 5)
33 int GLES2Util::GLGetNumValuesReturned(int id) const {
34 switch (id) {
35 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
36 case GL_ACTIVE_TEXTURE:
37 return 1;
38 case GL_ALIASED_LINE_WIDTH_RANGE:
39 return 2;
40 case GL_ALIASED_POINT_SIZE_RANGE:
41 return 2;
42 case GL_ALPHA_BITS:
43 return 1;
44 case GL_ARRAY_BUFFER_BINDING:
45 return 1;
46 case GL_BLEND:
47 return 1;
48 case GL_BLEND_COLOR:
49 return 4;
50 case GL_BLEND_DST_ALPHA:
51 return 1;
52 case GL_BLEND_DST_RGB:
53 return 1;
54 case GL_BLEND_EQUATION_ALPHA:
55 return 1;
56 case GL_BLEND_EQUATION_RGB:
57 return 1;
58 case GL_BLEND_SRC_ALPHA:
59 return 1;
60 case GL_BLEND_SRC_RGB:
61 return 1;
62 case GL_BLUE_BITS:
63 return 1;
64 case GL_COLOR_CLEAR_VALUE:
65 return 4;
66 case GL_COLOR_WRITEMASK:
67 return 4;
68 case GL_COMPRESSED_TEXTURE_FORMATS:
69 return num_compressed_texture_formats_;
70 case GL_CULL_FACE:
71 return 1;
72 case GL_CULL_FACE_MODE:
73 return 1;
74 case GL_CURRENT_PROGRAM:
75 return 1;
76 case GL_DEPTH_BITS:
77 return 1;
78 case GL_DEPTH_CLEAR_VALUE:
79 return 1;
80 case GL_DEPTH_FUNC:
81 return 1;
82 case GL_DEPTH_RANGE:
83 return 2;
84 case GL_DEPTH_TEST:
85 return 1;
86 case GL_DEPTH_WRITEMASK:
87 return 1;
88 case GL_DITHER:
89 return 1;
90 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
91 return 1;
92 case GL_FRAMEBUFFER_BINDING:
93 return 1;
94 case GL_FRONT_FACE:
95 return 1;
96 case GL_GENERATE_MIPMAP_HINT:
97 return 1;
98 case GL_GREEN_BITS:
99 return 1;
100 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
101 return 1;
102 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
103 return 1;
104 case GL_LINE_WIDTH:
105 return 1;
106 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
107 return 1;
108 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
109 return 1;
110 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
111 return 1;
112 case GL_MAX_RENDERBUFFER_SIZE:
113 return 1;
114 case GL_MAX_TEXTURE_IMAGE_UNITS:
115 return 1;
116 case GL_MAX_TEXTURE_SIZE:
117 return 1;
118 case GL_MAX_VARYING_VECTORS:
119 return 1;
120 case GL_MAX_VERTEX_ATTRIBS:
121 return 1;
122 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
123 return 1;
124 case GL_MAX_VERTEX_UNIFORM_VECTORS:
125 return 1;
126 case GL_MAX_VIEWPORT_DIMS:
127 return 2;
128 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
129 return 1;
130 case GL_NUM_SHADER_BINARY_FORMATS:
131 return 1;
132 case GL_PACK_ALIGNMENT:
133 return 1;
134 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
135 return 1;
136 case GL_POLYGON_OFFSET_FACTOR:
137 return 1;
138 case GL_POLYGON_OFFSET_FILL:
139 return 1;
140 case GL_POLYGON_OFFSET_UNITS:
141 return 1;
142 case GL_RED_BITS:
143 return 1;
144 case GL_RENDERBUFFER_BINDING:
145 return 1;
146 case GL_SAMPLE_BUFFERS:
147 return 1;
148 case GL_SAMPLE_COVERAGE_INVERT:
149 return 1;
150 case GL_SAMPLE_COVERAGE_VALUE:
151 return 1;
152 case GL_SAMPLES:
153 return 1;
154 case GL_SCISSOR_BOX:
155 return 4;
156 case GL_SCISSOR_TEST:
157 return 1;
158 case GL_SHADER_BINARY_FORMATS:
159 return num_shader_binary_formats_;
160 case GL_SHADER_COMPILER:
161 return 1;
162 case GL_STENCIL_BACK_FAIL:
163 return 1;
164 case GL_STENCIL_BACK_FUNC:
165 return 1;
166 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
167 return 1;
168 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
169 return 1;
170 case GL_STENCIL_BACK_REF:
171 return 1;
172 case GL_STENCIL_BACK_VALUE_MASK:
173 return 1;
174 case GL_STENCIL_BACK_WRITEMASK:
175 return 1;
176 case GL_STENCIL_BITS:
177 return 1;
178 case GL_STENCIL_CLEAR_VALUE:
179 return 1;
180 case GL_STENCIL_FAIL:
181 return 1;
182 case GL_STENCIL_FUNC:
183 return 1;
184 case GL_STENCIL_PASS_DEPTH_FAIL:
185 return 1;
186 case GL_STENCIL_PASS_DEPTH_PASS:
187 return 1;
188 case GL_STENCIL_REF:
189 return 1;
190 case GL_STENCIL_TEST:
191 return 1;
192 case GL_STENCIL_VALUE_MASK:
193 return 1;
194 case GL_STENCIL_WRITEMASK:
195 return 1;
196 case GL_SUBPIXEL_BITS:
197 return 1;
198 case GL_TEXTURE_BINDING_2D:
199 return 1;
200 case GL_TEXTURE_BINDING_CUBE_MAP:
201 return 1;
202 case GL_TEXTURE_BINDING_EXTERNAL_OES:
203 return 1;
204 case GL_TEXTURE_BINDING_RECTANGLE_ARB:
205 return 1;
206 case GL_TEXTURE_IMMUTABLE_FORMAT_EXT:
207 return 1;
208 case GL_UNPACK_ALIGNMENT:
209 return 1;
210 case GL_VIEWPORT:
211 return 4;
212 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
213 // GL_CHROMIUM_framebuffer_multisample
214 case GL_MAX_SAMPLES_EXT:
215 return 1;
216 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
217 return 1;
219 // -- glGetBufferParameteriv
220 case GL_BUFFER_SIZE:
221 return 1;
222 case GL_BUFFER_USAGE:
223 return 1;
225 // -- glGetFramebufferAttachmentParameteriv
226 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
227 return 1;
228 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
229 return 1;
230 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
231 return 1;
232 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
233 return 1;
234 // -- glGetFramebufferAttachmentParameteriv with
235 // GL_EXT_multisampled_render_to_texture
236 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT:
237 return 1;
238 // -- glGetFramebufferAttachmentParameteriv with
239 // GL_EXT_sRGB
240 case GL_FRAMEBUFFER_ATTACHMENT_COLOR_ENCODING_EXT:
241 return 1;
243 // -- glGetProgramiv
244 case GL_DELETE_STATUS:
245 return 1;
246 case GL_LINK_STATUS:
247 return 1;
248 case GL_VALIDATE_STATUS:
249 return 1;
250 case GL_INFO_LOG_LENGTH:
251 return 1;
252 case GL_ATTACHED_SHADERS:
253 return 1;
254 case GL_ACTIVE_ATTRIBUTES:
255 return 1;
256 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
257 return 1;
258 case GL_ACTIVE_UNIFORMS:
259 return 1;
260 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
261 return 1;
264 // -- glGetRenderbufferAttachmentParameteriv
265 case GL_RENDERBUFFER_WIDTH:
266 return 1;
267 case GL_RENDERBUFFER_HEIGHT:
268 return 1;
269 case GL_RENDERBUFFER_INTERNAL_FORMAT:
270 return 1;
271 case GL_RENDERBUFFER_RED_SIZE:
272 return 1;
273 case GL_RENDERBUFFER_GREEN_SIZE:
274 return 1;
275 case GL_RENDERBUFFER_BLUE_SIZE:
276 return 1;
277 case GL_RENDERBUFFER_ALPHA_SIZE:
278 return 1;
279 case GL_RENDERBUFFER_DEPTH_SIZE:
280 return 1;
281 case GL_RENDERBUFFER_STENCIL_SIZE:
282 return 1;
283 // -- glGetRenderbufferAttachmentParameteriv with
284 // GL_EXT_multisampled_render_to_texture
285 case GL_RENDERBUFFER_SAMPLES_EXT:
286 return 1;
288 // -- glGetShaderiv
289 case GL_SHADER_TYPE:
290 return 1;
291 // Already defined under glGetFramebufferAttachemntParameteriv.
292 // case GL_DELETE_STATUS:
293 // return 1;
294 case GL_COMPILE_STATUS:
295 return 1;
296 // Already defined under glGetFramebufferAttachemntParameteriv.
297 // case GL_INFO_LOG_LENGTH:
298 // return 1;
299 case GL_SHADER_SOURCE_LENGTH:
300 return 1;
301 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
302 return 1;
304 // -- glGetTexParameterfv, glGetTexParameteriv
305 case GL_TEXTURE_MAG_FILTER:
306 return 1;
307 case GL_TEXTURE_MIN_FILTER:
308 return 1;
309 case GL_TEXTURE_WRAP_S:
310 return 1;
311 case GL_TEXTURE_WRAP_T:
312 return 1;
313 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
314 return 1;
316 // -- glGetVertexAttribfv, glGetVertexAttribiv
317 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
318 return 1;
319 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
320 return 1;
321 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
322 return 1;
323 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
324 return 1;
325 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
326 return 1;
327 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
328 return 1;
329 case GL_CURRENT_VERTEX_ATTRIB:
330 return 4;
332 // -- glHint with GL_OES_standard_derivatives
333 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
334 return 1;
336 // Chromium internal bind_generates_resource query
337 case GL_BIND_GENERATES_RESOURCE_CHROMIUM:
338 return 1;
340 // bad enum
341 default:
342 return 0;
346 namespace {
348 // Return the number of elements per group of a specified format.
349 int ElementsPerGroup(int format, int type) {
350 switch (type) {
351 case GL_UNSIGNED_SHORT_5_6_5:
352 case GL_UNSIGNED_SHORT_4_4_4_4:
353 case GL_UNSIGNED_SHORT_5_5_5_1:
354 case GL_UNSIGNED_INT_24_8_OES:
355 case GL_UNSIGNED_INT_2_10_10_10_REV:
356 case GL_UNSIGNED_INT_10F_11F_11F_REV:
357 case GL_UNSIGNED_INT_5_9_9_9_REV:
358 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
359 return 1;
360 default:
361 break;
364 switch (format) {
365 case GL_RGB:
366 case GL_RGB_INTEGER:
367 case GL_SRGB_EXT:
368 return 3;
369 case GL_LUMINANCE_ALPHA:
370 case GL_RG_EXT:
371 case GL_RG_INTEGER:
372 return 2;
373 case GL_RGBA:
374 case GL_RGBA_INTEGER:
375 case GL_BGRA_EXT:
376 case GL_SRGB_ALPHA_EXT:
377 return 4;
378 case GL_ALPHA:
379 case GL_LUMINANCE:
380 case GL_DEPTH_COMPONENT:
381 case GL_DEPTH_COMPONENT24_OES:
382 case GL_DEPTH_COMPONENT32_OES:
383 case GL_DEPTH_COMPONENT16:
384 case GL_DEPTH24_STENCIL8_OES:
385 case GL_DEPTH_STENCIL_OES:
386 case GL_RED_EXT:
387 case GL_RED_INTEGER:
388 return 1;
389 default:
390 return 0;
394 // Return the number of bytes per element, based on the element type.
395 int BytesPerElement(int type) {
396 switch (type) {
397 case GL_FLOAT:
398 case GL_UNSIGNED_INT_24_8_OES:
399 case GL_UNSIGNED_INT:
400 case GL_UNSIGNED_INT_2_10_10_10_REV:
401 case GL_UNSIGNED_INT_10F_11F_11F_REV:
402 case GL_UNSIGNED_INT_5_9_9_9_REV:
403 case GL_FLOAT_32_UNSIGNED_INT_24_8_REV:
404 return 4;
405 case GL_HALF_FLOAT_OES:
406 case GL_UNSIGNED_SHORT:
407 case GL_SHORT:
408 case GL_UNSIGNED_SHORT_5_6_5:
409 case GL_UNSIGNED_SHORT_4_4_4_4:
410 case GL_UNSIGNED_SHORT_5_5_5_1:
411 return 2;
412 case GL_UNSIGNED_BYTE:
413 case GL_BYTE:
414 return 1;
415 default:
416 return 0;
420 } // anonymous namespace
422 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
423 return BytesPerElement(type) * ElementsPerGroup(format, type);
426 bool GLES2Util::ComputeImagePaddedRowSize(
427 int width, int format, int type, int unpack_alignment,
428 uint32* padded_row_size) {
429 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
430 uint32 unpadded_row_size;
431 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
432 return false;
434 uint32 temp;
435 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
436 return false;
438 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
439 return true;
442 // Returns the amount of data glTexImage*D or glTexSubImage*D will access.
443 bool GLES2Util::ComputeImageDataSizes(
444 int width, int height, int depth, int format, int type,
445 int unpack_alignment, uint32* size, uint32* ret_unpadded_row_size,
446 uint32* ret_padded_row_size) {
447 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
448 uint32 row_size;
449 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
450 return false;
452 uint32 num_of_rows;
453 if (!SafeMultiplyUint32(height, depth, &num_of_rows)) {
454 return false;
456 if (num_of_rows > 1) {
457 uint32 temp;
458 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
459 return false;
461 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
462 uint32 size_of_all_but_last_row;
463 if (!SafeMultiplyUint32((num_of_rows - 1), padded_row_size,
464 &size_of_all_but_last_row)) {
465 return false;
467 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
468 return false;
470 if (ret_padded_row_size) {
471 *ret_padded_row_size = padded_row_size;
473 } else {
474 *size = row_size;
475 if (ret_padded_row_size) {
476 *ret_padded_row_size = row_size;
479 if (ret_unpadded_row_size) {
480 *ret_unpadded_row_size = row_size;
483 return true;
486 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
487 switch (format) {
488 case GL_STENCIL_INDEX8:
489 return 1;
490 case GL_RGBA4:
491 case GL_RGB565:
492 case GL_RGB5_A1:
493 case GL_DEPTH_COMPONENT16:
494 return 2;
495 case GL_RGB:
496 case GL_RGBA:
497 case GL_DEPTH24_STENCIL8_OES:
498 case GL_RGB8_OES:
499 case GL_RGBA8_OES:
500 case GL_DEPTH_COMPONENT24_OES:
501 return 4;
502 default:
503 return 0;
507 uint32 GLES2Util::GetGLDataTypeSizeForUniforms(int type) {
508 switch (type) {
509 case GL_FLOAT:
510 return sizeof(GLfloat); // NOLINT
511 case GL_FLOAT_VEC2:
512 return sizeof(GLfloat) * 2; // NOLINT
513 case GL_FLOAT_VEC3:
514 return sizeof(GLfloat) * 3; // NOLINT
515 case GL_FLOAT_VEC4:
516 return sizeof(GLfloat) * 4; // NOLINT
517 case GL_INT:
518 return sizeof(GLint); // NOLINT
519 case GL_INT_VEC2:
520 return sizeof(GLint) * 2; // NOLINT
521 case GL_INT_VEC3:
522 return sizeof(GLint) * 3; // NOLINT
523 case GL_INT_VEC4:
524 return sizeof(GLint) * 4; // NOLINT
525 case GL_BOOL:
526 return sizeof(GLint); // NOLINT
527 case GL_BOOL_VEC2:
528 return sizeof(GLint) * 2; // NOLINT
529 case GL_BOOL_VEC3:
530 return sizeof(GLint) * 3; // NOLINT
531 case GL_BOOL_VEC4:
532 return sizeof(GLint) * 4; // NOLINT
533 case GL_FLOAT_MAT2:
534 return sizeof(GLfloat) * 2 * 2; // NOLINT
535 case GL_FLOAT_MAT3:
536 return sizeof(GLfloat) * 3 * 3; // NOLINT
537 case GL_FLOAT_MAT4:
538 return sizeof(GLfloat) * 4 * 4; // NOLINT
539 case GL_SAMPLER_2D:
540 return sizeof(GLint); // NOLINT
541 case GL_SAMPLER_2D_RECT_ARB:
542 return sizeof(GLint); // NOLINT
543 case GL_SAMPLER_CUBE:
544 return sizeof(GLint); // NOLINT
545 case GL_SAMPLER_EXTERNAL_OES:
546 return sizeof(GLint); // NOLINT
547 default:
548 return 0;
552 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
553 switch (type) {
554 case GL_BYTE:
555 return sizeof(GLbyte); // NOLINT
556 case GL_UNSIGNED_BYTE:
557 return sizeof(GLubyte); // NOLINT
558 case GL_SHORT:
559 return sizeof(GLshort); // NOLINT
560 case GL_UNSIGNED_SHORT:
561 return sizeof(GLushort); // NOLINT
562 case GL_INT:
563 return sizeof(GLint); // NOLINT
564 case GL_UNSIGNED_INT:
565 return sizeof(GLuint); // NOLINT
566 case GL_FLOAT:
567 return sizeof(GLfloat); // NOLINT
568 case GL_FIXED:
569 return sizeof(GLfixed); // NOLINT
570 default:
571 return 0;
575 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
576 switch (error) {
577 case GL_INVALID_ENUM:
578 return gl_error_bit::kInvalidEnum;
579 case GL_INVALID_VALUE:
580 return gl_error_bit::kInvalidValue;
581 case GL_INVALID_OPERATION:
582 return gl_error_bit::kInvalidOperation;
583 case GL_OUT_OF_MEMORY:
584 return gl_error_bit::kOutOfMemory;
585 case GL_INVALID_FRAMEBUFFER_OPERATION:
586 return gl_error_bit::kInvalidFrameBufferOperation;
587 case GL_CONTEXT_LOST_KHR:
588 return gl_error_bit::kContextLost;
589 default:
590 NOTREACHED();
591 return gl_error_bit::kNoError;
595 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
596 switch (error_bit) {
597 case gl_error_bit::kInvalidEnum:
598 return GL_INVALID_ENUM;
599 case gl_error_bit::kInvalidValue:
600 return GL_INVALID_VALUE;
601 case gl_error_bit::kInvalidOperation:
602 return GL_INVALID_OPERATION;
603 case gl_error_bit::kOutOfMemory:
604 return GL_OUT_OF_MEMORY;
605 case gl_error_bit::kInvalidFrameBufferOperation:
606 return GL_INVALID_FRAMEBUFFER_OPERATION;
607 case gl_error_bit::kContextLost:
608 return GL_CONTEXT_LOST_KHR;
609 default:
610 NOTREACHED();
611 return GL_NO_ERROR;
615 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
616 static uint32 faces[] = {
617 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
618 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
619 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
620 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
621 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
622 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
624 return faces[index];
627 size_t GLES2Util::GLTargetToFaceIndex(uint32 target) {
628 switch (target) {
629 case GL_TEXTURE_2D:
630 case GL_TEXTURE_EXTERNAL_OES:
631 case GL_TEXTURE_RECTANGLE_ARB:
632 return 0;
633 case GL_TEXTURE_CUBE_MAP_POSITIVE_X:
634 return 0;
635 case GL_TEXTURE_CUBE_MAP_NEGATIVE_X:
636 return 1;
637 case GL_TEXTURE_CUBE_MAP_POSITIVE_Y:
638 return 2;
639 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Y:
640 return 3;
641 case GL_TEXTURE_CUBE_MAP_POSITIVE_Z:
642 return 4;
643 case GL_TEXTURE_CUBE_MAP_NEGATIVE_Z:
644 return 5;
645 default:
646 NOTREACHED();
647 return 0;
651 uint32 GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format) {
652 switch (internal_format) {
653 case GL_RGB16F_EXT:
654 case GL_RGB32F_EXT:
655 return GL_RGB;
656 case GL_RGBA16F_EXT:
657 case GL_RGBA32F_EXT:
658 return GL_RGBA;
659 default:
660 return GL_RGBA;
664 uint32 GLES2Util::GetPreferredGLReadPixelsType(
665 uint32 internal_format, uint32 texture_type) {
666 switch (internal_format) {
667 case GL_RGBA32F_EXT:
668 case GL_RGB32F_EXT:
669 return GL_FLOAT;
670 case GL_RGBA16F_EXT:
671 case GL_RGB16F_EXT:
672 return GL_HALF_FLOAT_OES;
673 case GL_RGBA:
674 case GL_RGB:
675 // Unsized internal format, check the type
676 switch (texture_type) {
677 case GL_FLOAT:
678 case GL_HALF_FLOAT_OES:
679 return GL_FLOAT;
680 default:
681 return GL_UNSIGNED_BYTE;
683 default:
684 return GL_UNSIGNED_BYTE;
688 uint32 GLES2Util::GetChannelsForFormat(int format) {
689 switch (format) {
690 case GL_ALPHA:
691 case GL_ALPHA16F_EXT:
692 case GL_ALPHA32F_EXT:
693 return kAlpha;
694 case GL_LUMINANCE:
695 return kRGB;
696 case GL_LUMINANCE_ALPHA:
697 return kRGBA;
698 case GL_RGB:
699 case GL_RGB8_OES:
700 case GL_RGB565:
701 case GL_RGB16F_EXT:
702 case GL_RGB32F_EXT:
703 case GL_SRGB_EXT:
704 return kRGB;
705 case GL_BGRA_EXT:
706 case GL_BGRA8_EXT:
707 case GL_RGBA16F_EXT:
708 case GL_RGBA32F_EXT:
709 case GL_RGBA:
710 case GL_RGBA8_OES:
711 case GL_RGBA4:
712 case GL_RGB5_A1:
713 case GL_SRGB_ALPHA_EXT:
714 case GL_SRGB8_ALPHA8_EXT:
715 return kRGBA;
716 case GL_DEPTH_COMPONENT32_OES:
717 case GL_DEPTH_COMPONENT24_OES:
718 case GL_DEPTH_COMPONENT16:
719 case GL_DEPTH_COMPONENT:
720 return kDepth;
721 case GL_STENCIL_INDEX8:
722 return kStencil;
723 case GL_DEPTH_STENCIL_OES:
724 case GL_DEPTH24_STENCIL8_OES:
725 return kDepth | kStencil;
726 case GL_RED_EXT:
727 return kRed;
728 case GL_RG_EXT:
729 return kRed | kGreen;
730 default:
731 return 0x0000;
735 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
736 int type, uint32 max_color_attachments) {
737 switch (type) {
738 case GL_DEPTH_ATTACHMENT:
739 return kDepth;
740 case GL_STENCIL_ATTACHMENT:
741 return kStencil;
742 default:
743 if (type >= GL_COLOR_ATTACHMENT0 &&
744 type < static_cast<int>(
745 GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
746 return kRGBA;
748 return 0x0000;
752 std::string GLES2Util::GetStringEnum(uint32 value) {
753 const EnumToString* entry = enum_to_string_table_;
754 const EnumToString* end = entry + enum_to_string_table_len_;
755 for (;entry < end; ++entry) {
756 if (value == entry->value) {
757 return entry->name;
760 std::stringstream ss;
761 ss.fill('0');
762 ss.width(value < 0x10000 ? 4 : 8);
763 ss << std::hex << value;
764 return "0x" + ss.str();
767 std::string GLES2Util::GetStringError(uint32 value) {
768 static EnumToString string_table[] = {
769 { GL_NONE, "GL_NONE" },
771 return GLES2Util::GetQualifiedEnumString(
772 string_table, arraysize(string_table), value);
775 std::string GLES2Util::GetStringBool(uint32 value) {
776 return value ? "GL_TRUE" : "GL_FALSE";
779 std::string GLES2Util::GetQualifiedEnumString(
780 const EnumToString* table, size_t count, uint32 value) {
781 for (const EnumToString* end = table + count; table < end; ++table) {
782 if (table->value == value) {
783 return table->name;
786 return GetStringEnum(value);
789 bool GLES2Util::ParseUniformName(
790 const std::string& name,
791 size_t* array_pos,
792 int* element_index,
793 bool* getting_array) {
794 if (name.empty())
795 return false;
796 bool getting_array_location = false;
797 size_t open_pos = std::string::npos;
798 base::CheckedNumeric<int> index = 0;
799 if (name[name.size() - 1] == ']') {
800 if (name.size() < 3) {
801 return false;
803 open_pos = name.find_last_of('[');
804 if (open_pos == std::string::npos ||
805 open_pos >= name.size() - 2) {
806 return false;
808 size_t last = name.size() - 1;
809 for (size_t pos = open_pos + 1; pos < last; ++pos) {
810 int8 digit = name[pos] - '0';
811 if (digit < 0 || digit > 9) {
812 return false;
814 index = index * 10 + digit;
816 if (!index.IsValid()) {
817 return false;
819 getting_array_location = true;
821 *getting_array = getting_array_location;
822 *element_index = index.ValueOrDie();
823 *array_pos = open_pos;
824 return true;
827 size_t GLES2Util::CalcClearBufferivDataCount(int buffer) {
828 switch (buffer) {
829 case GL_COLOR:
830 return 4;
831 case GL_STENCIL:
832 return 1;
833 default:
834 return 0;
838 size_t GLES2Util::CalcClearBufferfvDataCount(int buffer) {
839 switch (buffer) {
840 case GL_COLOR:
841 return 4;
842 case GL_DEPTH:
843 return 1;
844 default:
845 return 0;
849 // static
850 void GLES2Util::MapUint64ToTwoUint32(
851 uint64_t v64, uint32_t* v32_0, uint32_t* v32_1) {
852 DCHECK(v32_0 && v32_1);
853 *v32_0 = static_cast<uint32_t>(v64 & 0xFFFFFFFF);
854 *v32_1 = static_cast<uint32_t>((v64 & 0xFFFFFFFF00000000) >> 32);
857 // static
858 uint64_t GLES2Util::MapTwoUint32ToUint64(uint32_t v32_0, uint32_t v32_1) {
859 uint64_t v64 = v32_1;
860 return (v64 << 32) | v32_0;
863 namespace {
865 // WebGraphicsContext3DCommandBufferImpl configuration attributes. Those in
866 // the 16-bit range are the same as used by EGL. Those outside the 16-bit range
867 // are unique to Chromium. Attributes are matched using a closest fit algorithm.
869 // From <EGL/egl.h>.
870 const int32 kAlphaSize = 0x3021; // EGL_ALPHA_SIZE
871 const int32 kBlueSize = 0x3022; // EGL_BLUE_SIZE
872 const int32 kGreenSize = 0x3023; // EGL_GREEN_SIZE
873 const int32 kRedSize = 0x3024; // EGL_RED_SIZE
874 const int32 kDepthSize = 0x3025; // EGL_DEPTH_SIZE
875 const int32 kStencilSize = 0x3026; // EGL_STENCIL_SIZE
876 const int32 kSamples = 0x3031; // EGL_SAMPLES
877 const int32 kSampleBuffers = 0x3032; // EGL_SAMPLE_BUFFERS
878 const int32 kNone = 0x3038; // EGL_NONE
879 const int32 kSwapBehavior = 0x3093; // EGL_SWAP_BEHAVIOR
880 const int32 kBufferPreserved = 0x3094; // EGL_BUFFER_PRESERVED
881 const int32 kBufferDestroyed = 0x3095; // EGL_BUFFER_DESTROYED
883 // Chromium only.
884 const int32 kBindGeneratesResource = 0x10000;
885 const int32 kFailIfMajorPerfCaveat = 0x10001;
886 const int32 kLoseContextWhenOutOfMemory = 0x10002;
888 } // namespace
890 ContextCreationAttribHelper::ContextCreationAttribHelper()
891 : alpha_size(-1),
892 blue_size(-1),
893 green_size(-1),
894 red_size(-1),
895 depth_size(-1),
896 stencil_size(-1),
897 samples(-1),
898 sample_buffers(-1),
899 buffer_preserved(true),
900 bind_generates_resource(true),
901 fail_if_major_perf_caveat(false),
902 lose_context_when_out_of_memory(false) {}
904 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) const {
905 if (alpha_size != -1) {
906 attribs->push_back(kAlphaSize);
907 attribs->push_back(alpha_size);
909 if (blue_size != -1) {
910 attribs->push_back(kBlueSize);
911 attribs->push_back(blue_size);
913 if (green_size != -1) {
914 attribs->push_back(kGreenSize);
915 attribs->push_back(green_size);
917 if (red_size != -1) {
918 attribs->push_back(kRedSize);
919 attribs->push_back(red_size);
921 if (depth_size != -1) {
922 attribs->push_back(kDepthSize);
923 attribs->push_back(depth_size);
925 if (stencil_size != -1) {
926 attribs->push_back(kStencilSize);
927 attribs->push_back(stencil_size);
929 if (samples != -1) {
930 attribs->push_back(kSamples);
931 attribs->push_back(samples);
933 if (sample_buffers != -1) {
934 attribs->push_back(kSampleBuffers);
935 attribs->push_back(sample_buffers);
937 attribs->push_back(kSwapBehavior);
938 attribs->push_back(buffer_preserved ? kBufferPreserved : kBufferDestroyed);
939 attribs->push_back(kBindGeneratesResource);
940 attribs->push_back(bind_generates_resource ? 1 : 0);
941 attribs->push_back(kFailIfMajorPerfCaveat);
942 attribs->push_back(fail_if_major_perf_caveat ? 1 : 0);
943 attribs->push_back(kLoseContextWhenOutOfMemory);
944 attribs->push_back(lose_context_when_out_of_memory ? 1 : 0);
945 attribs->push_back(kNone);
948 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
949 for (size_t i = 0; i < attribs.size(); i += 2) {
950 const int32 attrib = attribs[i];
951 if (i + 1 >= attribs.size()) {
952 if (attrib == kNone) {
953 return true;
956 DLOG(ERROR) << "Missing value after context creation attribute: "
957 << attrib;
958 return false;
961 const int32 value = attribs[i+1];
962 switch (attrib) {
963 case kAlphaSize:
964 alpha_size = value;
965 break;
966 case kBlueSize:
967 blue_size = value;
968 break;
969 case kGreenSize:
970 green_size = value;
971 break;
972 case kRedSize:
973 red_size = value;
974 break;
975 case kDepthSize:
976 depth_size = value;
977 break;
978 case kStencilSize:
979 stencil_size = value;
980 break;
981 case kSamples:
982 samples = value;
983 break;
984 case kSampleBuffers:
985 sample_buffers = value;
986 break;
987 case kSwapBehavior:
988 buffer_preserved = value == kBufferPreserved;
989 break;
990 case kBindGeneratesResource:
991 bind_generates_resource = value != 0;
992 break;
993 case kFailIfMajorPerfCaveat:
994 fail_if_major_perf_caveat = value != 0;
995 break;
996 case kLoseContextWhenOutOfMemory:
997 lose_context_when_out_of_memory = value != 0;
998 break;
999 case kNone:
1000 // Terminate list, even if more attributes.
1001 return true;
1002 default:
1003 DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
1004 return false;
1008 return true;
1011 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
1013 } // namespace gles2
1014 } // namespace gpu