• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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.
4 
5 // This file is here so other GLES2 related files can have a common set of
6 // includes where appropriate.
7 
8 #include <stdio.h>
9 #include <GLES2/gl2.h>
10 #include <GLES2/gl2ext.h>
11 #include <GLES2/gl2extchromium.h>
12 
13 #include "gpu/command_buffer/common/gles2_cmd_format.h"
14 #include "gpu/command_buffer/common/gles2_cmd_utils.h"
15 
16 namespace gpu {
17 namespace gles2 {
18 
19 namespace gl_error_bit {
20 enum GLErrorBit {
21   kNoError = 0,
22   kInvalidEnum = (1 << 0),
23   kInvalidValue = (1 << 1),
24   kInvalidOperation = (1 << 2),
25   kOutOfMemory = (1 << 3),
26   kInvalidFrameBufferOperation = (1 << 4)
27 };
28 }
29 
GLGetNumValuesReturned(int id) const30 int GLES2Util::GLGetNumValuesReturned(int id) const {
31   switch (id) {
32     // -- glGetBooleanv, glGetFloatv, glGetIntergerv
33     case GL_ACTIVE_TEXTURE:
34       return 1;
35     case GL_ALIASED_LINE_WIDTH_RANGE:
36       return 2;
37     case GL_ALIASED_POINT_SIZE_RANGE:
38       return 2;
39     case GL_ALPHA_BITS:
40       return 1;
41     case GL_ARRAY_BUFFER_BINDING:
42       return 1;
43     case GL_BLEND:
44       return 1;
45     case GL_BLEND_COLOR:
46       return 4;
47     case GL_BLEND_DST_ALPHA:
48       return 1;
49     case GL_BLEND_DST_RGB:
50       return 1;
51     case GL_BLEND_EQUATION_ALPHA:
52       return 1;
53     case GL_BLEND_EQUATION_RGB:
54       return 1;
55     case GL_BLEND_SRC_ALPHA:
56       return 1;
57     case GL_BLEND_SRC_RGB:
58       return 1;
59     case GL_BLUE_BITS:
60       return 1;
61     case GL_COLOR_CLEAR_VALUE:
62       return 4;
63     case GL_COLOR_WRITEMASK:
64       return 4;
65     case GL_COMPRESSED_TEXTURE_FORMATS:
66       return num_compressed_texture_formats_;
67     case GL_CULL_FACE:
68       return 1;
69     case GL_CULL_FACE_MODE:
70       return 1;
71     case GL_CURRENT_PROGRAM:
72       return 1;
73     case GL_DEPTH_BITS:
74       return 1;
75     case GL_DEPTH_CLEAR_VALUE:
76       return 1;
77     case GL_DEPTH_FUNC:
78       return 1;
79     case GL_DEPTH_RANGE:
80       return 2;
81     case GL_DEPTH_TEST:
82       return 1;
83     case GL_DEPTH_WRITEMASK:
84       return 1;
85     case GL_DITHER:
86       return 1;
87     case GL_ELEMENT_ARRAY_BUFFER_BINDING:
88       return 1;
89     case GL_FRAMEBUFFER_BINDING:
90       return 1;
91     case GL_FRONT_FACE:
92       return 1;
93     case GL_GENERATE_MIPMAP_HINT:
94       return 1;
95     case GL_GREEN_BITS:
96       return 1;
97     case GL_IMPLEMENTATION_COLOR_READ_FORMAT:
98       return 1;
99     case GL_IMPLEMENTATION_COLOR_READ_TYPE:
100       return 1;
101     case GL_LINE_WIDTH:
102       return 1;
103     case GL_MAX_COMBINED_TEXTURE_IMAGE_UNITS:
104       return 1;
105     case GL_MAX_CUBE_MAP_TEXTURE_SIZE:
106       return 1;
107     case GL_MAX_FRAGMENT_UNIFORM_VECTORS:
108       return 1;
109     case GL_MAX_RENDERBUFFER_SIZE:
110       return 1;
111     case GL_MAX_TEXTURE_IMAGE_UNITS:
112       return 1;
113     case GL_MAX_TEXTURE_SIZE:
114       return 1;
115     case GL_MAX_VARYING_VECTORS:
116       return 1;
117     case GL_MAX_VERTEX_ATTRIBS:
118       return 1;
119     case GL_MAX_VERTEX_TEXTURE_IMAGE_UNITS:
120       return 1;
121     case GL_MAX_VERTEX_UNIFORM_VECTORS:
122       return 1;
123     case GL_MAX_VIEWPORT_DIMS:
124       return 2;
125     case GL_NUM_COMPRESSED_TEXTURE_FORMATS:
126       return 1;
127     case GL_NUM_SHADER_BINARY_FORMATS:
128       return 1;
129     case GL_PACK_ALIGNMENT:
130       return 1;
131     case GL_PACK_REVERSE_ROW_ORDER_ANGLE:
132       return 1;
133     case GL_POLYGON_OFFSET_FACTOR:
134       return 1;
135     case GL_POLYGON_OFFSET_FILL:
136       return 1;
137     case GL_POLYGON_OFFSET_UNITS:
138       return 1;
139     case GL_RED_BITS:
140       return 1;
141     case GL_RENDERBUFFER_BINDING:
142       return 1;
143     case GL_SAMPLE_BUFFERS:
144       return 1;
145     case GL_SAMPLE_COVERAGE_INVERT:
146       return 1;
147     case GL_SAMPLE_COVERAGE_VALUE:
148       return 1;
149     case GL_SAMPLES:
150       return 1;
151     case GL_SCISSOR_BOX:
152       return 4;
153     case GL_SCISSOR_TEST:
154       return 1;
155     case GL_SHADER_BINARY_FORMATS:
156       return num_shader_binary_formats_;
157     case GL_SHADER_COMPILER:
158       return 1;
159     case GL_STENCIL_BACK_FAIL:
160       return 1;
161     case GL_STENCIL_BACK_FUNC:
162       return 1;
163     case GL_STENCIL_BACK_PASS_DEPTH_FAIL:
164       return 1;
165     case GL_STENCIL_BACK_PASS_DEPTH_PASS:
166       return 1;
167     case GL_STENCIL_BACK_REF:
168       return 1;
169     case GL_STENCIL_BACK_VALUE_MASK:
170       return 1;
171     case GL_STENCIL_BACK_WRITEMASK:
172       return 1;
173     case GL_STENCIL_BITS:
174       return 1;
175     case GL_STENCIL_CLEAR_VALUE:
176       return 1;
177     case GL_STENCIL_FAIL:
178       return 1;
179     case GL_STENCIL_FUNC:
180       return 1;
181     case GL_STENCIL_PASS_DEPTH_FAIL:
182       return 1;
183     case GL_STENCIL_PASS_DEPTH_PASS:
184       return 1;
185     case GL_STENCIL_REF:
186       return 1;
187     case GL_STENCIL_TEST:
188       return 1;
189     case GL_STENCIL_VALUE_MASK:
190       return 1;
191     case GL_STENCIL_WRITEMASK:
192       return 1;
193     case GL_SUBPIXEL_BITS:
194       return 1;
195     case GL_TEXTURE_BINDING_2D:
196       return 1;
197     case GL_TEXTURE_BINDING_CUBE_MAP:
198       return 1;
199     case GL_TEXTURE_BINDING_EXTERNAL_OES:
200       return 1;
201     case GL_TEXTURE_BINDING_RECTANGLE_ARB:
202       return 1;
203     case GL_TEXTURE_IMMUTABLE_FORMAT_EXT:
204       return 1;
205     case GL_UNPACK_ALIGNMENT:
206       return 1;
207     case GL_VIEWPORT:
208       return 4;
209     // -- glGetBooleanv, glGetFloatv, glGetIntergerv with
210     //    GL_CHROMIUM_framebuffer_multisample
211     case GL_MAX_SAMPLES_EXT:
212       return 1;
213     case GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT:
214       return 1;
215 
216     // -- glGetBufferParameteriv
217     case GL_BUFFER_SIZE:
218       return 1;
219     case GL_BUFFER_USAGE:
220       return 1;
221 
222     // -- glGetFramebufferAttachmentParameteriv
223     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_TYPE:
224       return 1;
225     case GL_FRAMEBUFFER_ATTACHMENT_OBJECT_NAME:
226       return 1;
227     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_LEVEL:
228       return 1;
229     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_CUBE_MAP_FACE:
230       return 1;
231     // -- glGetFramebufferAttachmentParameteriv with
232     //    GL_EXT_multisampled_render_to_texture
233     case GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT:
234       return 1;
235 
236     // -- glGetProgramiv
237     case GL_DELETE_STATUS:
238       return 1;
239     case GL_LINK_STATUS:
240       return 1;
241     case GL_VALIDATE_STATUS:
242       return 1;
243     case GL_INFO_LOG_LENGTH:
244       return 1;
245     case GL_ATTACHED_SHADERS:
246       return 1;
247     case GL_ACTIVE_ATTRIBUTES:
248       return 1;
249     case GL_ACTIVE_ATTRIBUTE_MAX_LENGTH:
250       return 1;
251     case GL_ACTIVE_UNIFORMS:
252       return 1;
253     case GL_ACTIVE_UNIFORM_MAX_LENGTH:
254       return 1;
255 
256 
257     // -- glGetRenderbufferAttachmentParameteriv
258     case GL_RENDERBUFFER_WIDTH:
259       return 1;
260     case GL_RENDERBUFFER_HEIGHT:
261       return 1;
262     case GL_RENDERBUFFER_INTERNAL_FORMAT:
263       return 1;
264     case GL_RENDERBUFFER_RED_SIZE:
265       return 1;
266     case GL_RENDERBUFFER_GREEN_SIZE:
267       return 1;
268     case GL_RENDERBUFFER_BLUE_SIZE:
269       return 1;
270     case GL_RENDERBUFFER_ALPHA_SIZE:
271       return 1;
272     case GL_RENDERBUFFER_DEPTH_SIZE:
273       return 1;
274     case GL_RENDERBUFFER_STENCIL_SIZE:
275       return 1;
276     // -- glGetRenderbufferAttachmentParameteriv with
277     //    GL_EXT_multisampled_render_to_texture
278     case GL_RENDERBUFFER_SAMPLES_EXT:
279       return 1;
280 
281     // -- glGetShaderiv
282     case GL_SHADER_TYPE:
283       return 1;
284     // Already defined under glGetFramebufferAttachemntParameteriv.
285     // case GL_DELETE_STATUS:
286     //   return 1;
287     case GL_COMPILE_STATUS:
288       return 1;
289     // Already defined under glGetFramebufferAttachemntParameteriv.
290     // case GL_INFO_LOG_LENGTH:
291     //   return 1;
292     case GL_SHADER_SOURCE_LENGTH:
293       return 1;
294     case GL_TRANSLATED_SHADER_SOURCE_LENGTH_ANGLE:
295       return 1;
296 
297     // -- glGetTexParameterfv, glGetTexParameteriv
298     case GL_TEXTURE_MAG_FILTER:
299       return 1;
300     case GL_TEXTURE_MIN_FILTER:
301       return 1;
302     case GL_TEXTURE_WRAP_S:
303       return 1;
304     case GL_TEXTURE_WRAP_T:
305       return 1;
306     case GL_TEXTURE_MAX_ANISOTROPY_EXT:
307       return 1;
308 
309     // -- glGetVertexAttribfv, glGetVertexAttribiv
310     case GL_VERTEX_ATTRIB_ARRAY_BUFFER_BINDING:
311       return 1;
312     case GL_VERTEX_ATTRIB_ARRAY_ENABLED:
313       return 1;
314     case GL_VERTEX_ATTRIB_ARRAY_SIZE:
315       return 1;
316     case GL_VERTEX_ATTRIB_ARRAY_STRIDE:
317       return 1;
318     case GL_VERTEX_ATTRIB_ARRAY_TYPE:
319       return 1;
320     case GL_VERTEX_ATTRIB_ARRAY_NORMALIZED:
321       return 1;
322     case GL_CURRENT_VERTEX_ATTRIB:
323       return 4;
324 
325     // -- glHint with GL_OES_standard_derivatives
326     case GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES:
327       return 1;
328 
329     // bad enum
330     default:
331       return 0;
332   }
333 }
334 
335 namespace {
336 
337 // Return the number of elements per group of a specified format.
ElementsPerGroup(int format,int type)338 int ElementsPerGroup(int format, int type) {
339   switch (type) {
340     case GL_UNSIGNED_SHORT_5_6_5:
341     case GL_UNSIGNED_SHORT_4_4_4_4:
342     case GL_UNSIGNED_SHORT_5_5_5_1:
343     case GL_UNSIGNED_INT_24_8_OES:
344        return 1;
345     default:
346        break;
347     }
348 
349     switch (format) {
350     case GL_RGB:
351        return 3;
352     case GL_LUMINANCE_ALPHA:
353        return 2;
354     case GL_RGBA:
355     case GL_BGRA_EXT:
356        return 4;
357     case GL_ALPHA:
358     case GL_LUMINANCE:
359     case GL_DEPTH_COMPONENT:
360     case GL_DEPTH_COMPONENT24_OES:
361     case GL_DEPTH_COMPONENT32_OES:
362     case GL_DEPTH_COMPONENT16:
363     case GL_DEPTH24_STENCIL8_OES:
364     case GL_DEPTH_STENCIL_OES:
365        return 1;
366     default:
367        return 0;
368   }
369 }
370 
371 // Return the number of bytes per element, based on the element type.
BytesPerElement(int type)372 int BytesPerElement(int type) {
373   switch (type) {
374     case GL_FLOAT:
375     case GL_UNSIGNED_INT_24_8_OES:
376     case GL_UNSIGNED_INT:
377       return 4;
378     case GL_HALF_FLOAT_OES:
379     case GL_UNSIGNED_SHORT:
380     case GL_SHORT:
381     case GL_UNSIGNED_SHORT_5_6_5:
382     case GL_UNSIGNED_SHORT_4_4_4_4:
383     case GL_UNSIGNED_SHORT_5_5_5_1:
384        return 2;
385     case GL_UNSIGNED_BYTE:
386     case GL_BYTE:
387        return 1;
388     default:
389        return 0;
390   }
391 }
392 
393 }  // anonymous namespace
394 
ComputeImageGroupSize(int format,int type)395 uint32 GLES2Util::ComputeImageGroupSize(int format, int type) {
396   return BytesPerElement(type) * ElementsPerGroup(format, type);
397 }
398 
ComputeImagePaddedRowSize(int width,int format,int type,int unpack_alignment,uint32 * padded_row_size)399 bool GLES2Util::ComputeImagePaddedRowSize(
400         int width, int format, int type, int unpack_alignment,
401         uint32* padded_row_size) {
402   uint32 bytes_per_group = ComputeImageGroupSize(format, type);
403   uint32 unpadded_row_size;
404   if (!SafeMultiplyUint32(width, bytes_per_group, &unpadded_row_size)) {
405     return false;
406   }
407   uint32 temp;
408   if (!SafeAddUint32(unpadded_row_size, unpack_alignment - 1, &temp)) {
409       return false;
410   }
411   *padded_row_size = (temp / unpack_alignment) * unpack_alignment;
412   return true;
413 }
414 
415 // Returns the amount of data glTexImage2D or glTexSubImage2D will access.
ComputeImageDataSizes(int width,int height,int format,int type,int unpack_alignment,uint32 * size,uint32 * ret_unpadded_row_size,uint32 * ret_padded_row_size)416 bool GLES2Util::ComputeImageDataSizes(
417     int width, int height, int format, int type, int unpack_alignment,
418     uint32* size, uint32* ret_unpadded_row_size, uint32* ret_padded_row_size) {
419   uint32 bytes_per_group = ComputeImageGroupSize(format, type);
420   uint32 row_size;
421   if (!SafeMultiplyUint32(width, bytes_per_group, &row_size)) {
422     return false;
423   }
424   if (height > 1) {
425     uint32 temp;
426     if (!SafeAddUint32(row_size, unpack_alignment - 1, &temp)) {
427       return false;
428     }
429     uint32 padded_row_size = (temp / unpack_alignment) * unpack_alignment;
430     uint32 size_of_all_but_last_row;
431     if (!SafeMultiplyUint32((height - 1), padded_row_size,
432                             &size_of_all_but_last_row)) {
433       return false;
434     }
435     if (!SafeAddUint32(size_of_all_but_last_row, row_size, size)) {
436       return false;
437     }
438     if (ret_padded_row_size) {
439       *ret_padded_row_size = padded_row_size;
440     }
441   } else {
442     if (!SafeMultiplyUint32(height, row_size, size)) {
443       return false;
444     }
445     if (ret_padded_row_size) {
446       *ret_padded_row_size = row_size;
447     }
448   }
449   if (ret_unpadded_row_size) {
450     *ret_unpadded_row_size = row_size;
451   }
452 
453   return true;
454 }
455 
RenderbufferBytesPerPixel(int format)456 size_t GLES2Util::RenderbufferBytesPerPixel(int format) {
457   switch (format) {
458     case GL_STENCIL_INDEX8:
459       return 1;
460     case GL_RGBA4:
461     case GL_RGB565:
462     case GL_RGB5_A1:
463     case GL_DEPTH_COMPONENT16:
464       return 2;
465     case GL_RGB:
466     case GL_RGBA:
467     case GL_DEPTH24_STENCIL8_OES:
468     case GL_RGB8_OES:
469     case GL_RGBA8_OES:
470     case GL_DEPTH_COMPONENT24_OES:
471       return 4;
472     default:
473       return 0;
474   }
475 }
476 
GetGLDataTypeSizeForUniforms(int type)477 uint32 GLES2Util::GetGLDataTypeSizeForUniforms(int type) {
478   switch (type) {
479     case GL_FLOAT:
480       return sizeof(GLfloat);              // NOLINT
481     case GL_FLOAT_VEC2:
482       return sizeof(GLfloat) * 2;          // NOLINT
483     case GL_FLOAT_VEC3:
484       return sizeof(GLfloat) * 3;          // NOLINT
485     case GL_FLOAT_VEC4:
486       return sizeof(GLfloat) * 4;          // NOLINT
487     case GL_INT:
488       return sizeof(GLint);                // NOLINT
489     case GL_INT_VEC2:
490       return sizeof(GLint) * 2;            // NOLINT
491     case GL_INT_VEC3:
492       return sizeof(GLint) * 3;            // NOLINT
493     case GL_INT_VEC4:
494       return sizeof(GLint) * 4;            // NOLINT
495     case GL_BOOL:
496       return sizeof(GLint);                // NOLINT
497     case GL_BOOL_VEC2:
498       return sizeof(GLint) * 2;            // NOLINT
499     case GL_BOOL_VEC3:
500       return sizeof(GLint) * 3;            // NOLINT
501     case GL_BOOL_VEC4:
502       return sizeof(GLint) * 4;            // NOLINT
503     case GL_FLOAT_MAT2:
504       return sizeof(GLfloat) * 2 * 2;      // NOLINT
505     case GL_FLOAT_MAT3:
506       return sizeof(GLfloat) * 3 * 3;      // NOLINT
507     case GL_FLOAT_MAT4:
508       return sizeof(GLfloat) * 4 * 4;      // NOLINT
509     case GL_SAMPLER_2D:
510       return sizeof(GLint);                // NOLINT
511     case GL_SAMPLER_2D_RECT_ARB:
512       return sizeof(GLint);                // NOLINT
513     case GL_SAMPLER_CUBE:
514       return sizeof(GLint);                // NOLINT
515     case GL_SAMPLER_EXTERNAL_OES:
516       return sizeof(GLint);                // NOLINT
517     default:
518       return 0;
519   }
520 }
521 
GetGLTypeSizeForTexturesAndBuffers(uint32 type)522 size_t GLES2Util::GetGLTypeSizeForTexturesAndBuffers(uint32 type) {
523   switch (type) {
524     case GL_BYTE:
525       return sizeof(GLbyte);  // NOLINT
526     case GL_UNSIGNED_BYTE:
527       return sizeof(GLubyte);  // NOLINT
528     case GL_SHORT:
529       return sizeof(GLshort);  // NOLINT
530     case GL_UNSIGNED_SHORT:
531       return sizeof(GLushort);  // NOLINT
532     case GL_INT:
533       return sizeof(GLint);  // NOLINT
534     case GL_UNSIGNED_INT:
535       return sizeof(GLuint);  // NOLINT
536     case GL_FLOAT:
537       return sizeof(GLfloat);  // NOLINT
538     case GL_FIXED:
539       return sizeof(GLfixed);  // NOLINT
540     default:
541       return 0;
542   }
543 }
544 
GLErrorToErrorBit(uint32 error)545 uint32 GLES2Util::GLErrorToErrorBit(uint32 error) {
546   switch (error) {
547     case GL_INVALID_ENUM:
548       return gl_error_bit::kInvalidEnum;
549     case GL_INVALID_VALUE:
550       return gl_error_bit::kInvalidValue;
551     case GL_INVALID_OPERATION:
552       return gl_error_bit::kInvalidOperation;
553     case GL_OUT_OF_MEMORY:
554       return gl_error_bit::kOutOfMemory;
555     case GL_INVALID_FRAMEBUFFER_OPERATION:
556       return gl_error_bit::kInvalidFrameBufferOperation;
557     default:
558       NOTREACHED();
559       return gl_error_bit::kNoError;
560   }
561 }
562 
GLErrorBitToGLError(uint32 error_bit)563 uint32 GLES2Util::GLErrorBitToGLError(uint32 error_bit) {
564   switch (error_bit) {
565     case gl_error_bit::kInvalidEnum:
566       return GL_INVALID_ENUM;
567     case gl_error_bit::kInvalidValue:
568       return GL_INVALID_VALUE;
569     case gl_error_bit::kInvalidOperation:
570       return GL_INVALID_OPERATION;
571     case gl_error_bit::kOutOfMemory:
572       return GL_OUT_OF_MEMORY;
573     case gl_error_bit::kInvalidFrameBufferOperation:
574       return GL_INVALID_FRAMEBUFFER_OPERATION;
575     default:
576       NOTREACHED();
577       return GL_NO_ERROR;
578   }
579 }
580 
IndexToGLFaceTarget(int index)581 uint32 GLES2Util::IndexToGLFaceTarget(int index) {
582   static uint32 faces[] = {
583     GL_TEXTURE_CUBE_MAP_POSITIVE_X,
584     GL_TEXTURE_CUBE_MAP_NEGATIVE_X,
585     GL_TEXTURE_CUBE_MAP_POSITIVE_Y,
586     GL_TEXTURE_CUBE_MAP_NEGATIVE_Y,
587     GL_TEXTURE_CUBE_MAP_POSITIVE_Z,
588     GL_TEXTURE_CUBE_MAP_NEGATIVE_Z,
589   };
590   return faces[index];
591 }
592 
GetPreferredGLReadPixelsFormat(uint32 internal_format)593 uint32 GLES2Util::GetPreferredGLReadPixelsFormat(uint32 internal_format) {
594   switch (internal_format) {
595     case GL_RGB16F_EXT:
596     case GL_RGB32F_EXT:
597       return GL_RGB;
598     case GL_RGBA16F_EXT:
599     case GL_RGBA32F_EXT:
600       return GL_RGBA;
601     default:
602       return GL_RGBA;
603   }
604 }
605 
GetPreferredGLReadPixelsType(uint32 internal_format,uint32 texture_type)606 uint32 GLES2Util::GetPreferredGLReadPixelsType(
607     uint32 internal_format, uint32 texture_type) {
608   switch (internal_format) {
609     case GL_RGBA32F_EXT:
610     case GL_RGB32F_EXT:
611       return GL_FLOAT;
612     case GL_RGBA16F_EXT:
613     case GL_RGB16F_EXT:
614       return GL_HALF_FLOAT_OES;
615     case GL_RGBA:
616     case GL_RGB:
617       // Unsized internal format, check the type
618       switch (texture_type) {
619         case GL_FLOAT:
620         case GL_HALF_FLOAT_OES:
621           return GL_FLOAT;
622         default:
623           return GL_UNSIGNED_BYTE;
624       }
625     default:
626       return GL_UNSIGNED_BYTE;
627   }
628 }
629 
GetChannelsForFormat(int format)630 uint32 GLES2Util::GetChannelsForFormat(int format) {
631   switch (format) {
632     case GL_ALPHA:
633     case GL_ALPHA16F_EXT:
634     case GL_ALPHA32F_EXT:
635       return kAlpha;
636     case GL_LUMINANCE:
637       return kRGB;
638     case GL_LUMINANCE_ALPHA:
639       return kRGBA;
640     case GL_RGB:
641     case GL_RGB8_OES:
642     case GL_RGB565:
643     case GL_RGB16F_EXT:
644     case GL_RGB32F_EXT:
645       return kRGB;
646     case GL_BGRA_EXT:
647     case GL_BGRA8_EXT:
648     case GL_RGBA16F_EXT:
649     case GL_RGBA32F_EXT:
650     case GL_RGBA:
651     case GL_RGBA8_OES:
652     case GL_RGBA4:
653     case GL_RGB5_A1:
654       return kRGBA;
655     case GL_DEPTH_COMPONENT32_OES:
656     case GL_DEPTH_COMPONENT24_OES:
657     case GL_DEPTH_COMPONENT16:
658     case GL_DEPTH_COMPONENT:
659       return kDepth;
660     case GL_STENCIL_INDEX8:
661       return kStencil;
662     case GL_DEPTH_STENCIL_OES:
663     case GL_DEPTH24_STENCIL8_OES:
664       return kDepth | kStencil;
665     default:
666       return 0x0000;
667   }
668 }
669 
GetChannelsNeededForAttachmentType(int type,uint32 max_color_attachments)670 uint32 GLES2Util::GetChannelsNeededForAttachmentType(
671     int type, uint32 max_color_attachments) {
672   switch (type) {
673     case GL_DEPTH_ATTACHMENT:
674       return kDepth;
675     case GL_STENCIL_ATTACHMENT:
676       return kStencil;
677     default:
678       if (type >= GL_COLOR_ATTACHMENT0 &&
679           type < static_cast<int>(
680               GL_COLOR_ATTACHMENT0 + max_color_attachments)) {
681         return kRGBA;
682       }
683       return 0x0000;
684   }
685 }
686 
GetStringEnum(uint32 value)687 std::string GLES2Util::GetStringEnum(uint32 value) {
688   const EnumToString* entry = enum_to_string_table_;
689   const EnumToString* end = entry + enum_to_string_table_len_;
690   for (;entry < end; ++entry) {
691     if (value == entry->value) {
692       return entry->name;
693     }
694   }
695   char buffer[20];
696   sprintf(buffer, (value < 0x10000) ? "0x%04x" : "0x%08x", value);
697   return buffer;
698 }
699 
GetStringError(uint32 value)700 std::string GLES2Util::GetStringError(uint32 value) {
701   static EnumToString string_table[] = {
702     { GL_NONE, "GL_NONE" },
703   };
704   return GLES2Util::GetQualifiedEnumString(
705       string_table, arraysize(string_table), value);
706 }
707 
GetStringBool(uint32 value)708 std::string GLES2Util::GetStringBool(uint32 value) {
709   return value ? "GL_TRUE" : "GL_FALSE";
710 }
711 
GetQualifiedEnumString(const EnumToString * table,size_t count,uint32 value)712 std::string GLES2Util::GetQualifiedEnumString(
713     const EnumToString* table, size_t count, uint32 value) {
714   for (const EnumToString* end = table + count; table < end; ++table) {
715     if (table->value == value) {
716       return table->name;
717     }
718   }
719   return GetStringEnum(value);
720 }
721 
ParseUniformName(const std::string & name,size_t * array_pos,int * element_index,bool * getting_array)722 bool GLES2Util::ParseUniformName(
723     const std::string& name,
724     size_t* array_pos,
725     int* element_index,
726     bool* getting_array) {
727   bool getting_array_location = false;
728   size_t open_pos = std::string::npos;
729   int index = 0;
730   if (name[name.size() - 1] == ']') {
731     if (name.size() < 3) {
732       return false;
733     }
734     open_pos = name.find_last_of('[');
735     if (open_pos == std::string::npos ||
736         open_pos >= name.size() - 2) {
737       return false;
738     }
739     size_t last = name.size() - 1;
740     for (size_t pos = open_pos + 1; pos < last; ++pos) {
741       int8 digit = name[pos] - '0';
742       if (digit < 0 || digit > 9) {
743         return false;
744       }
745       index = index * 10 + digit;
746     }
747     getting_array_location = true;
748   }
749   *getting_array = getting_array_location;
750   *element_index = index;
751   *array_pos = open_pos;
752   return true;
753 }
754 
755 namespace {
756 
757 // From <EGL/egl.h>.
758 const int32 kAlphaSize       = 0x3021;  // EGL_ALPHA_SIZE
759 const int32 kBlueSize        = 0x3022;  // EGL_BLUE_SIZE
760 const int32 kGreenSize       = 0x3023;  // EGL_GREEN_SIZE
761 const int32 kRedSize         = 0x3024;  // EGL_RED_SIZE
762 const int32 kDepthSize       = 0x3025;  // EGL_DEPTH_SIZE
763 const int32 kStencilSize     = 0x3026;  // EGL_STENCIL_SIZE
764 const int32 kSamples         = 0x3031;  // EGL_SAMPLES
765 const int32 kSampleBuffers   = 0x3032;  // EGL_SAMPLE_BUFFERS
766 const int32 kNone            = 0x3038;  // EGL_NONE
767 const int32 kSwapBehavior    = 0x3093;  // EGL_SWAP_BEHAVIOR
768 const int32 kBufferPreserved = 0x3094;  // EGL_BUFFER_PRESERVED
769 const int32 kBufferDestroyed = 0x3095;  // EGL_BUFFER_DESTROYED
770 
771 // Chromium only.
772 const int32 kShareResources        = 0x10000;
773 const int32 kBindGeneratesResource = 0x10001;
774 const int32 kFailIfMajorPerfCaveat = 0x10002;
775 
776 }  // namespace
777 
ContextCreationAttribHelper()778 ContextCreationAttribHelper::ContextCreationAttribHelper()
779   : alpha_size_(-1),
780     blue_size_(-1),
781     green_size_(-1),
782     red_size_(-1),
783     depth_size_(-1),
784     stencil_size_(-1),
785     samples_(-1),
786     sample_buffers_(-1),
787     buffer_preserved_(true),
788     share_resources_(false),
789     bind_generates_resource_(true),
790     fail_if_major_perf_caveat_(false) {
791 }
792 
Serialize(std::vector<int32> * attribs)793 void ContextCreationAttribHelper::Serialize(std::vector<int32>* attribs) {
794   if (alpha_size_ != -1) {
795     attribs->push_back(kAlphaSize);
796     attribs->push_back(alpha_size_);
797   }
798   if (blue_size_ != -1) {
799     attribs->push_back(kBlueSize);
800     attribs->push_back(blue_size_);
801   }
802   if (green_size_ != -1) {
803     attribs->push_back(kGreenSize);
804     attribs->push_back(green_size_);
805   }
806   if (red_size_ != -1) {
807     attribs->push_back(kRedSize);
808     attribs->push_back(red_size_);
809   }
810   if (depth_size_ != -1) {
811     attribs->push_back(kDepthSize);
812     attribs->push_back(depth_size_);
813   }
814   if (stencil_size_ != -1) {
815     attribs->push_back(kStencilSize);
816     attribs->push_back(stencil_size_);
817   }
818   if (samples_ != -1) {
819     attribs->push_back(kSamples);
820     attribs->push_back(samples_);
821   }
822   if (sample_buffers_ != -1) {
823     attribs->push_back(kSampleBuffers);
824     attribs->push_back(sample_buffers_);
825   }
826   attribs->push_back(kSwapBehavior);
827   attribs->push_back(buffer_preserved_ ? kBufferPreserved : kBufferDestroyed);
828   attribs->push_back(kShareResources);
829   attribs->push_back(share_resources_ ? 1 : 0);
830   attribs->push_back(kBindGeneratesResource);
831   attribs->push_back(bind_generates_resource_ ? 1 : 0);
832   attribs->push_back(kFailIfMajorPerfCaveat);
833   attribs->push_back(fail_if_major_perf_caveat_ ? 1 : 0);
834   attribs->push_back(kNone);
835 }
836 
Parse(const std::vector<int32> & attribs)837 bool ContextCreationAttribHelper::Parse(const std::vector<int32>& attribs) {
838   for (size_t i = 0; i < attribs.size(); i += 2) {
839     const int32 attrib = attribs[i];
840     if (i + 1 >= attribs.size()) {
841       if (attrib == kNone) {
842         return true;
843       }
844 
845       DLOG(ERROR) << "Missing value after context creation attribute: "
846                   << attrib;
847       return false;
848     }
849 
850     const int32 value = attribs[i+1];
851     switch (attrib) {
852       case kAlphaSize:
853         alpha_size_ = value;
854         break;
855       case kBlueSize:
856         blue_size_ = value;
857         break;
858       case kGreenSize:
859         green_size_ = value;
860         break;
861       case kRedSize:
862         red_size_ = value;
863         break;
864       case kDepthSize:
865         depth_size_ = value;
866         break;
867       case kStencilSize:
868         stencil_size_ = value;
869         break;
870       case kSamples:
871         samples_ = value;
872         break;
873       case kSampleBuffers:
874         sample_buffers_ = value;
875         break;
876       case kSwapBehavior:
877         buffer_preserved_ = value == kBufferPreserved;
878         break;
879       case kShareResources:
880         share_resources_ = value != 0;
881         break;
882       case kBindGeneratesResource:
883         bind_generates_resource_ = value != 0;
884         break;
885       case kFailIfMajorPerfCaveat:
886         fail_if_major_perf_caveat_ = value != 0;
887         break;
888       case kNone:
889         // Terminate list, even if more attributes.
890         return true;
891       default:
892         DLOG(ERROR) << "Invalid context creation attribute: " << attrib;
893         return false;
894     }
895   }
896 
897   return true;
898 }
899 
900 #include "gpu/command_buffer/common/gles2_cmd_utils_implementation_autogen.h"
901 
902 }  // namespace gles2
903 }  // namespace gpu
904 
905