Telemetry: adding memory tests & solving problems on the go.
[chromium-blink-merge.git] / gpu / command_buffer / common / gles2_cmd_utils.cc
blobb6b268c9e3e91059076938c7ed69dd524b62556c
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 <stdio.h>
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
12 #include "../common/gles2_cmd_utils.h"
13 #include "../common/gles2_cmd_format.h"
15 namespace gpu {
16 namespace gles2 {
18 namespace gl_error_bit {
19 enum GLErrorBit {
20 kNoError = 0,
21 kInvalidEnum = (1 << 0),
22 kInvalidValue = (1 << 1),
23 kInvalidOperation = (1 << 2),
24 kOutOfMemory = (1 << 3),
25 kInvalidFrameBufferOperation = (1 << 4)
29 int GLES2Util::GLGetNumValuesReturned(int id) const {
30 switch (id) {
31 // -- glGetBooleanv, glGetFloatv, glGetIntergerv
32 case GL_ACTIVE_TEXTURE:
33 return 1;
34 case GL_ALIASED_LINE_WIDTH_RANGE:
35 return 2;
36 case GL_ALIASED_POINT_SIZE_RANGE:
37 return 2;
38 case GL_ALPHA_BITS:
39 return 1;
40 case GL_ARRAY_BUFFER_BINDING:
41 return 1;
42 case GL_BLEND:
43 return 1;
44 case GL_BLEND_COLOR:
45 return 4;
46 case GL_BLEND_DST_ALPHA:
47 return 1;
48 case GL_BLEND_DST_RGB:
49 return 1;
50 case GL_BLEND_EQUATION_ALPHA:
51 return 1;
52 case GL_BLEND_EQUATION_RGB:
53 return 1;
54 case GL_BLEND_SRC_ALPHA:
55 return 1;
56 case GL_BLEND_SRC_RGB:
57 return 1;
58 case GL_BLUE_BITS:
59 return 1;
60 case GL_COLOR_CLEAR_VALUE:
61 return 4;
62 case GL_COLOR_WRITEMASK:
63 return 4;
64 case GL_COMPRESSED_TEXTURE_FORMATS:
65 return num_compressed_texture_formats_;
66 case GL_CULL_FACE:
67 return 1;
68 case GL_CULL_FACE_MODE:
69 return 1;
70 case GL_CURRENT_PROGRAM:
71 return 1;
72 case GL_DEPTH_BITS:
73 return 1;
74 case GL_DEPTH_CLEAR_VALUE:
75 return 1;
76 case GL_DEPTH_FUNC:
77 return 1;
78 case GL_DEPTH_RANGE:
79 return 2;
80 case GL_DEPTH_TEST:
81 return 1;
82 case GL_DEPTH_WRITEMASK:
83 return 1;
84 case GL_DITHER:
85 return 1;
86 case GL_ELEMENT_ARRAY_BUFFER_BINDING:
87 return 1;
88 case GL_FRAMEBUFFER_BINDING:
89 return 1;
90 case GL_FRONT_FACE:
91 return 1;
92 case GL_GENERATE_MIPMAP_HINT:
93 return 1;
94 case GL_GREEN_BITS:
95 return 1;
96 case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
97 return 1;
98 case GL_IMPLEMENTATION_COLOR_READ_TYPE:
99 return 1;
100 case GL_LINE_WIDTH:
101 return 1;
102 case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
103 return 1;
104 case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
105 return 1;
106 case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
107 return 1;
108 case GL_MAX_RENDERBUFFER_SIZE:
109 return 1;
110 case GL_MAX_TEXTURE_IMAGE_UNITS:
111 return 1;
112 case GL_MAX_TEXTURE_SIZE:
113 return 1;
114 case GL_MAX_VARYING_VECTORS:
115 return 1;
116 case GL_MAX_VERTEX_ATTRIBS:
117 return 1;
118 case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
119 return 1;
120 case GL_MAX_VERTEX_UNIFORM_VECTORS:
121 return 1;
122 case GL_MAX_VIEWPORT_DIMS:
123 return 2;
124 case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
125 return 1;
126 case GL_NUM_SHADER_BINARY_FORMATS:
127 return 1;
128 case GL_PACK_ALIGNMENT:
129 return 1;
130 case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
131 return 1;
132 case GL_POLYGON_OFFSET_FACTOR:
133 return 1;
134 case GL_POLYGON_OFFSET_FILL:
135 return 1;
136 case GL_POLYGON_OFFSET_UNITS:
137 return 1;
138 case GL_RED_BITS:
139 return 1;
140 case GL_RENDERBUFFER_BINDING:
141 return 1;
142 case GL_SAMPLE_BUFFERS:
143 return 1;
144 case GL_SAMPLE_COVERAGE_INVERT:
145 return 1;
146 case GL_SAMPLE_COVERAGE_VALUE:
147 return 1;
148 case GL_SAMPLES:
149 return 1;
150 case GL_SCISSOR_BOX:
151 return 4;
152 case GL_SCISSOR_TEST:
153 return 1;
154 case GL_SHADER_BINARY_FORMATS:
155 return num_shader_binary_formats_;
156 case GL_SHADER_COMPILER:
157 return 1;
158 case GL_STENCIL_BACK_FAIL:
159 return 1;
160 case GL_STENCIL_BACK_FUNC:
161 return 1;
162 case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
163 return 1;
164 case GL_STENCIL_BACK_PASS_DEPTH_PASS:
165 return 1;
166 case GL_STENCIL_BACK_REF:
167 return 1;
168 case GL_STENCIL_BACK_VALUE_MASK:
169 return 1;
170 case GL_STENCIL_BACK_WRITEMASK:
171 return 1;
172 case GL_STENCIL_BITS:
173 return 1;
174 case GL_STENCIL_CLEAR_VALUE:
175 return 1;
176 case GL_STENCIL_FAIL:
177 return 1;
178 case GL_STENCIL_FUNC:
179 return 1;
180 case GL_STENCIL_PASS_DEPTH_FAIL:
181 return 1;
182 case GL_STENCIL_PASS_DEPTH_PASS:
183 return 1;
184 case GL_STENCIL_REF:
185 return 1;
186 case GL_STENCIL_TEST:
187 return 1;
188 case GL_STENCIL_VALUE_MASK:
189 return 1;
190 case GL_STENCIL_WRITEMASK:
191 return 1;
192 case GL_SUBPIXEL_BITS:
193 return 1;
194 case GL_TEXTURE_BINDING_2D:
195 return 1;
196 case GL_TEXTURE_BINDING_CUBE_MAP:
197 return 1;
198 case GL_TEXTURE_BINDING_EXTERNAL_OES:
199 return 1;
200 case GL_TEXTURE_BINDING_RECTANGLE_ARB:
201 return 1;
202 case GL_TEXTURE_IMMUTABLE_FORMAT_EXT:
203 return 1;
204 case GL_UNPACK_ALIGNMENT:
205 return 1;
206 case GL_VIEWPORT:
207 return 4;
208 // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
209 // GL_CHROMIUM_framebuffer_multisample
210 case GL_MAX_SAMPLES_EXT:
211 return 1;
212 case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
213 return 1;
215 // -- glGetBufferParameteriv
216 case GL_BUFFER_SIZE:
217 return 1;
218 case GL_BUFFER_USAGE:
219 return 1;
221 // -- glGetFramebufferAttachmentParameteriv
222 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
223 return 1;
224 case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
225 return 1;
226 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
227 return 1;
228 case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
229 return 1;
231 // -- glGetFramebufferAttachmentParameteriv
232 case GL_DELETE_STATUS:
233 return 1;
234 case GL_LINK_STATUS:
235 return 1;
236 case GL_VALIDATE_STATUS:
237 return 1;
238 case GL_INFO_LOG_LENGTH:
239 return 1;
240 case GL_ATTACHED_SHADERS:
241 return 1;
242 case GL_ACTIVE_ATTRIBUTES:
243 return 1;
244 case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
245 return 1;
246 case GL_ACTIVE_UNIFORMS:
247 return 1;
248 case GL_ACTIVE_UNIFORM_MAX_LENGTH:
249 return 1;
252 // -- glGetRenderbufferAttachmentParameteriv
253 case GL_RENDERBUFFER_WIDTH:
254 return 1;
255 case GL_RENDERBUFFER_HEIGHT:
256 return 1;
257 case GL_RENDERBUFFER_INTERNAL_FORMAT:
258 return 1;
259 case GL_RENDERBUFFER_RED_SIZE:
260 return 1;
261 case GL_RENDERBUFFER_GREEN_SIZE:
262 return 1;
263 case GL_RENDERBUFFER_BLUE_SIZE:
264 return 1;
265 case GL_RENDERBUFFER_ALPHA_SIZE:
266 return 1;
267 case GL_RENDERBUFFER_DEPTH_SIZE:
268 return 1;
269 case GL_RENDERBUFFER_STENCIL_SIZE:
270 return 1;
272 // -- glGetShaderiv
273 case GL_SHADER_TYPE:
274 return 1;
275 // Already defined under glGetFramebufferAttachemntParameteriv.
276 // case GL_DELETE_STATUS:
277 // return 1;
278 case GL_COMPILE_STATUS:
279 return 1;
280 // Already defined under glGetFramebufferAttachemntParameteriv.
281 // case GL_INFO_LOG_LENGTH:
282 // return 1;
283 case GL_SHADER_SOURCE_LENGTH:
284 return 1;
285 case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
286 return 1;
288 // -- glGetTexParameterfv, glGetTexParameteriv
289 case GL_TEXTURE_MAG_FILTER:
290 return 1;
291 case GL_TEXTURE_MIN_FILTER:
292 return 1;
293 case GL_TEXTURE_WRAP_S:
294 return 1;
295 case GL_TEXTURE_WRAP_T:
296 return 1;
297 case GL_TEXTURE_MAX_ANISOTROPY_EXT:
298 return 1;
300 // -- glGetVertexAttribfv, glGetVertexAttribiv
301 case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
302 return 1;
303 case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
304 return 1;
305 case GL_VERTEX_ATTRIB_ARRAY_SIZE:
306 return 1;
307 case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
308 return 1;
309 case GL_VERTEX_ATTRIB_ARRAY_TYPE:
310 return 1;
311 case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
312 return 1;
313 case GL_CURRENT_VERTEX_ATTRIB:
314 return 4;
316 // -- glHint with GL_OES_standard_derivatives
317 case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
318 return 1;
320 // bad enum
321 default:
322 return 0;
326 namespace {
328 // Return the number of elements per group of a specified format.
329 int ElementsPerGroup(int format, int type) {
330 switch (type) {
331 case GL_UNSIGNED_SHORT_5_6_5:
332 case GL_UNSIGNED_SHORT_4_4_4_4:
333 case GL_UNSIGNED_SHORT_5_5_5_1:
334 case GL_UNSIGNED_INT_24_8_OES:
335 return 1;
336 default:
337 break;
340 switch (format) {
341 case GL_RGB:
342 return 3;
343 case GL_LUMINANCE_ALPHA:
344 return 2;
345 case GL_RGBA:
346 case GL_BGRA_EXT:
347 return 4;
348 case GL_ALPHA:
349 case GL_LUMINANCE:
350 case GL_DEPTH_COMPONENT:
351 case GL_DEPTH_COMPONENT24_OES:
352 case GL_DEPTH_COMPONENT32_OES:
353 case GL_DEPTH_COMPONENT16:
354 case GL_DEPTH24_STENCIL8_OES:
355 case GL_DEPTH_STENCIL_OES:
356 return 1;
357 default:
358 return 0;
362 // Return the number of bytes per element, based on the element type.
363 int BytesPerElement(int type) {
364 switch (type) {
365 case GL_FLOAT:
366 case GL_UNSIGNED_INT_24_8_OES:
367 case GL_UNSIGNED_INT:
368 return 4;
369 case GL_HALF_FLOAT_OES:
370 case GL_UNSIGNED_SHORT:
371 case GL_SHORT:
372 case GL_UNSIGNED_SHORT_5_6_5:
373 case GL_UNSIGNED_SHORT_4_4_4_4:
374 case GL_UNSIGNED_SHORT_5_5_5_1:
375 return 2;
376 case GL_UNSIGNED_BYTE:
377 case GL_BYTE:
378 return 1;
379 default:
380 return 0;
384 } // anonymous namespace
386 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
387 return BytesPerElement(type) * ElementsPerGroup(format, type);
390 bool GLES2Util::ComputeImagePaddedRowSize(
391 int width, int format, int type, int unpack_alignment,
392 uint32* padded_row_size) {
393 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
394 uint32 unpadded_row_size;
395 if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
396 return false;
398 uint32 temp;
399 if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
400 return false;
402 *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
403 return true;
406 // Returns the amount of data glTexImage2D or glTexSubImage2D will access.
407 bool GLES2Util::ComputeImageDataSizes(
408 int width, int height, int format, int type, int unpack_alignment,
409 uint32* size, uint32* ret_unpadded_row_size, uint32* ret_padded_row_size) {
410 uint32 bytes_per_group = ComputeImageGroupSize(format, type);
411 uint32 row_size;
412 if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
413 return false;
415 if (height > 1) {
416 uint32 temp;
417 if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
418 return false;
420 uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
421 uint32 size_of_all_but_last_row;
422 if (!SafeMultiplyUint32((height - 1), padded_row_size,
423 &size_of_all_but_last_row)) {
424 return false;
426 if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
427 return false;
429 if (ret_padded_row_size) {
430 *ret_padded_row_size = padded_row_size;
432 } else {
433 if (!SafeMultiplyUint32(height, row_size, size)) {
434 return false;
436 if (ret_padded_row_size) {
437 *ret_padded_row_size = row_size;
440 if (ret_unpadded_row_size) {
441 *ret_unpadded_row_size = row_size;
444 return true;
447 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
448 switch (format) {
449 case GL_STENCIL_INDEX8:
450 return 1;
451 case GL_RGBA4:
452 case GL_RGB565:
453 case GL_RGB5_A1:
454 case GL_DEPTH_COMPONENT16:
455 return 2;
456 case GL_RGB:
457 case GL_RGBA:
458 case GL_DEPTH24_STENCIL8_OES:
459 case GL_RGB8_OES:
460 case GL_RGBA8_OES:
461 case GL_DEPTH_COMPONENT24_OES:
462 return 4;
463 default:
464 return 0;
468 uint32 GLES2Util::GetGLDataTypeSizeForUniforms(int type) {
469 switch (type) {
470 case GL_FLOAT:
471 return sizeof(GLfloat); // NOLINT
472 case GL_FLOAT_VEC2:
473 return sizeof(GLfloat) * 2; // NOLINT
474 case GL_FLOAT_VEC3:
475 return sizeof(GLfloat) * 3; // NOLINT
476 case GL_FLOAT_VEC4:
477 return sizeof(GLfloat) * 4; // NOLINT
478 case GL_INT:
479 return sizeof(GLint); // NOLINT
480 case GL_INT_VEC2:
481 return sizeof(GLint) * 2; // NOLINT
482 case GL_INT_VEC3:
483 return sizeof(GLint) * 3; // NOLINT
484 case GL_INT_VEC4:
485 return sizeof(GLint) * 4; // NOLINT
486 case GL_BOOL:
487 return sizeof(GLint); // NOLINT
488 case GL_BOOL_VEC2:
489 return sizeof(GLint) * 2; // NOLINT
490 case GL_BOOL_VEC3:
491 return sizeof(GLint) * 3; // NOLINT
492 case GL_BOOL_VEC4:
493 return sizeof(GLint) * 4; // NOLINT
494 case GL_FLOAT_MAT2:
495 return sizeof(GLfloat) * 2 * 2; // NOLINT
496 case GL_FLOAT_MAT3:
497 return sizeof(GLfloat) * 3 * 3; // NOLINT
498 case GL_FLOAT_MAT4:
499 return sizeof(GLfloat) * 4 * 4; // NOLINT
500 case GL_SAMPLER_2D:
501 return sizeof(GLint); // NOLINT
502 case GL_SAMPLER_2D_RECT_ARB:
503 return sizeof(GLint); // NOLINT
504 case GL_SAMPLER_CUBE:
505 return sizeof(GLint); // NOLINT
506 case GL_SAMPLER_EXTERNAL_OES:
507 return sizeof(GLint); // NOLINT
508 default:
509 return 0;
513 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
514 switch (type) {
515 case GL_BYTE:
516 return sizeof(GLbyte); // NOLINT
517 case GL_UNSIGNED_BYTE:
518 return sizeof(GLubyte); // NOLINT
519 case GL_SHORT:
520 return sizeof(GLshort); // NOLINT
521 case GL_UNSIGNED_SHORT:
522 return sizeof(GLushort); // NOLINT
523 case GL_INT:
524 return sizeof(GLint); // NOLINT
525 case GL_UNSIGNED_INT:
526 return sizeof(GLuint); // NOLINT
527 case GL_FLOAT:
528 return sizeof(GLfloat); // NOLINT
529 case GL_FIXED:
530 return sizeof(GLfixed); // NOLINT
531 default:
532 return 0;
536 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
537 switch (error) {
538 case GL_INVALID_ENUM:
539 return gl_error_bit::kInvalidEnum;
540 case GL_INVALID_VALUE:
541 return gl_error_bit::kInvalidValue;
542 case GL_INVALID_OPERATION:
543 return gl_error_bit::kInvalidOperation;
544 case GL_OUT_OF_MEMORY:
545 return gl_error_bit::kOutOfMemory;
546 case GL_INVALID_FRAMEBUFFER_OPERATION:
547 return gl_error_bit::kInvalidFrameBufferOperation;
548 default:
549 GPU_NOTREACHED();
550 return gl_error_bit::kNoError;
554 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
555 switch (error_bit) {
556 case gl_error_bit::kInvalidEnum:
557 return GL_INVALID_ENUM;
558 case gl_error_bit::kInvalidValue:
559 return GL_INVALID_VALUE;
560 case gl_error_bit::kInvalidOperation:
561 return GL_INVALID_OPERATION;
562 case gl_error_bit::kOutOfMemory:
563 return GL_OUT_OF_MEMORY;
564 case gl_error_bit::kInvalidFrameBufferOperation:
565 return GL_INVALID_FRAMEBUFFER_OPERATION;
566 default:
567 GPU_NOTREACHED();
568 return GL_NO_ERROR;
572 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
573 static uint32 faces[] = {
574 GL_TEXTURE_CUBE_MAP_POSITIVE_X,
575 GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
576 GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
577 GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
578 GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
579 GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
581 return faces[index];
584 uint32 GLES2Util::GetChannelsForFormat(int format) {
585 switch (format) {
586 case GL_ALPHA:
587 case GL_ALPHA16F_EXT:
588 case GL_ALPHA32F_EXT:
589 return kAlpha;
590 case GL_LUMINANCE:
591 return kRGB;
592 case GL_LUMINANCE_ALPHA:
593 return kRGBA;
594 case GL_RGB:
595 case GL_RGB8_OES:
596 case GL_RGB565:
597 case GL_RGB16F_EXT:
598 case GL_RGB32F_EXT:
599 return kRGB;
600 case GL_BGRA_EXT:
601 case GL_BGRA8_EXT:
602 case GL_RGBA16F_EXT:
603 case GL_RGBA32F_EXT:
604 case GL_RGBA:
605 case GL_RGBA8_OES:
606 case GL_RGBA4:
607 case GL_RGB5_A1:
608 return kRGBA;
609 case GL_DEPTH_COMPONENT32_OES:
610 case GL_DEPTH_COMPONENT24_OES:
611 case GL_DEPTH_COMPONENT16:
612 case GL_DEPTH_COMPONENT:
613 return kDepth;
614 case GL_STENCIL_INDEX8:
615 return kStencil;
616 case GL_DEPTH_STENCIL_OES:
617 case GL_DEPTH24_STENCIL8_OES:
618 return kDepth | kStencil;
619 default:
620 return 0x0000;
624 uint32 GLES2Util::GetChannelsNeededForAttachmentType(int type) {
625 switch (type) {
626 case GL_COLOR_ATTACHMENT0:
627 return kRGBA;
628 case GL_DEPTH_ATTACHMENT:
629 return kDepth;
630 case GL_STENCIL_ATTACHMENT:
631 return kStencil;
632 default:
633 return 0x0000;
637 std::string GLES2Util::GetStringEnum(uint32 value) {
638 const EnumToString* entry = enum_to_string_table_;
639 const EnumToString* end = entry + enum_to_string_table_len_;
640 for (;entry < end; ++entry) {
641 if (value == entry->value) {
642 return entry->name;
645 char buffer[20];
646 sprintf(buffer, (value < 0x10000) ? "0x%04x" : "0x%08x", value);
647 return buffer;
650 std::string GLES2Util::GetStringError(uint32 value) {
651 static EnumToString string_table[] = {
652 { GL_NONE, "GL_NONE" },
654 return GLES2Util::GetQualifiedEnumString(
655 string_table, arraysize(string_table), value);
658 std::string GLES2Util::GetStringBool(uint32 value) {
659 return value ? "GL_TRUE" : "GL_FALSE";
662 std::string GLES2Util::GetQualifiedEnumString(
663 const EnumToString* table, size_t count, uint32 value) {
664 for (const EnumToString* end = table + count; table < end; ++table) {
665 if (table->value == value) {
666 return table->name;
669 return GetStringEnum(value);
672 bool GLES2Util::ParseUniformName(
673 const std::string& name,
674 size_t* array_pos,
675 int* element_index,
676 bool* getting_array) {
677 bool getting_array_location = false;
678 size_t open_pos = std::string::npos;
679 int index = 0;
680 if (name[name.size() - 1] == ']') {
681 if (name.size() < 3) {
682 return false;
684 open_pos = name.find_last_of('[');
685 if (open_pos == std::string::npos ||
686 open_pos >= name.size() - 2) {
687 return false;
689 size_t last = name.size() - 1;
690 for (size_t pos = open_pos + 1; pos < last; ++pos) {
691 int8 digit = name[pos] - '0';
692 if (digit < 0 || digit > 9) {
693 return false;
695 index = index * 10 + digit;
697 getting_array_location = true;
699 *getting_array = getting_array_location;
700 *element_index = index;
701 *array_pos = open_pos;
702 return true;
705 ContextCreationAttribParser::ContextCreationAttribParser()
706 : alpha_size_(-1),
707 blue_size_(-1),
708 green_size_(-1),
709 red_size_(-1),
710 depth_size_(-1),
711 stencil_size_(-1),
712 samples_(-1),
713 sample_buffers_(-1),
714 buffer_preserved_(true),
715 share_resources_(false),
716 bind_generates_resource_(true) {
719 bool ContextCreationAttribParser::Parse(const std::vector<int32>& attribs) {
720 // From <EGL/egl.h>.
721 const int32 EGL_ALPHA_SIZE = 0x3021;
722 const int32 EGL_BLUE_SIZE = 0x3022;
723 const int32 EGL_GREEN_SIZE = 0x3023;
724 const int32 EGL_RED_SIZE = 0x3024;
725 const int32 EGL_DEPTH_SIZE = 0x3025;
726 const int32 EGL_STENCIL_SIZE = 0x3026;
727 const int32 EGL_SAMPLES = 0x3031;
728 const int32 EGL_SAMPLE_BUFFERS = 0x3032;
729 const int32 EGL_NONE = 0x3038;
730 const int32 EGL_SWAP_BEHAVIOR = 0x3093;
731 const int32 EGL_BUFFER_PRESERVED = 0x3094;
733 // Chromium only.
734 const int32 SHARE_RESOURCES = 0x10000;
735 const int32 BIND_GENERATES_RESOURCES = 0x10001;
737 for (size_t i = 0; i < attribs.size(); i += 2) {
738 const int32 attrib = attribs[i];
739 if (i + 1 >= attribs.size()) {
740 if (attrib == EGL_NONE) {
741 return true;
744 GPU_DLOG(ERROR) << "Missing value after context creation attribute: "
745 << attrib;
746 return false;
749 const int32 value = attribs[i+1];
750 switch (attrib) {
751 case EGL_ALPHA_SIZE:
752 alpha_size_ = value;
753 break;
754 case EGL_BLUE_SIZE:
755 blue_size_ = value;
756 break;
757 case EGL_GREEN_SIZE:
758 green_size_ = value;
759 break;
760 case EGL_RED_SIZE:
761 red_size_ = value;
762 break;
763 case EGL_DEPTH_SIZE:
764 depth_size_ = value;
765 break;
766 case EGL_STENCIL_SIZE:
767 stencil_size_ = value;
768 break;
769 case EGL_SAMPLES:
770 samples_ = value;
771 break;
772 case EGL_SAMPLE_BUFFERS:
773 sample_buffers_ = value;
774 break;
775 case EGL_SWAP_BEHAVIOR:
776 buffer_preserved_ = value == EGL_BUFFER_PRESERVED;
777 break;
778 case SHARE_RESOURCES:
779 share_resources_ = value != 0;
780 break;
781 case BIND_GENERATES_RESOURCES:
782 bind_generates_resource_ = value != 0;
783 break;
784 case EGL_NONE:
785 // Terminate list, even if more attributes.
786 return true;
787 default:
788 GPU_DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
789 return false;
793 return true;
796 #include "../common/gles2_cmd_utils_implementation_autogen.h"
798 } // namespace gles2
799 } // namespace gpu