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#include "gpu/command_buffer/service/feature_info.h"
6
7#include <set>
8
9#include "base/command_line.h"
10#include "base/macros.h"
11#include "base/metrics/histogram.h"
12#include "base/strings/string_number_conversions.h"
13#include "base/strings/string_split.h"
14#include "base/strings/string_util.h"
15#include "gpu/command_buffer/service/gl_utils.h"
16#include "gpu/command_buffer/service/gpu_switches.h"
17#include "ui/gl/gl_fence.h"
18#include "ui/gl/gl_implementation.h"
19
20namespace gpu {
21namespace gles2 {
22
23namespace {
24
25struct FormatInfo {
26  GLenum format;
27  const GLenum* types;
28  size_t count;
29};
30
31class StringSet {
32 public:
33  StringSet() {}
34
35  StringSet(const char* s) {
36    Init(s);
37  }
38
39  StringSet(const std::string& str) {
40    Init(str);
41  }
42
43  void Init(const char* s) {
44    std::string str(s ? s : "");
45    Init(str);
46  }
47
48  void Init(const std::string& str) {
49    std::vector<std::string> tokens;
50    Tokenize(str, " ", &tokens);
51    string_set_.insert(tokens.begin(), tokens.end());
52  }
53
54  bool Contains(const char* s) {
55    return string_set_.find(s) != string_set_.end();
56  }
57
58  bool Contains(const std::string& s) {
59    return string_set_.find(s) != string_set_.end();
60  }
61
62 private:
63  std::set<std::string> string_set_;
64};
65
66// Process a string of wordaround type IDs (seperated by ',') and set up
67// the corresponding Workaround flags.
68void StringToWorkarounds(
69    const std::string& types, FeatureInfo::Workarounds* workarounds) {
70  DCHECK(workarounds);
71  std::vector<std::string> pieces;
72  base::SplitString(types, ',', &pieces);
73  for (size_t i = 0; i < pieces.size(); ++i) {
74    int number = 0;
75    bool succeed = base::StringToInt(pieces[i], &number);
76    DCHECK(succeed);
77    switch (number) {
78#define GPU_OP(type, name)    \
79  case gpu::type:             \
80    workarounds->name = true; \
81    break;
82      GPU_DRIVER_BUG_WORKAROUNDS(GPU_OP)
83#undef GPU_OP
84      default:
85        NOTIMPLEMENTED();
86    }
87  }
88  if (workarounds->max_texture_size_limit_4096)
89    workarounds->max_texture_size = 4096;
90  if (workarounds->max_cube_map_texture_size_limit_4096)
91    workarounds->max_cube_map_texture_size = 4096;
92  if (workarounds->max_cube_map_texture_size_limit_1024)
93    workarounds->max_cube_map_texture_size = 1024;
94  if (workarounds->max_cube_map_texture_size_limit_512)
95    workarounds->max_cube_map_texture_size = 512;
96
97  if (workarounds->max_fragment_uniform_vectors_32)
98    workarounds->max_fragment_uniform_vectors = 32;
99  if (workarounds->max_varying_vectors_16)
100    workarounds->max_varying_vectors = 16;
101  if (workarounds->max_vertex_uniform_vectors_256)
102    workarounds->max_vertex_uniform_vectors = 256;
103}
104
105}  // anonymous namespace.
106
107FeatureInfo::FeatureFlags::FeatureFlags()
108    : chromium_color_buffer_float_rgba(false),
109      chromium_color_buffer_float_rgb(false),
110      chromium_framebuffer_multisample(false),
111      chromium_sync_query(false),
112      use_core_framebuffer_multisample(false),
113      multisampled_render_to_texture(false),
114      use_img_for_multisampled_render_to_texture(false),
115      oes_standard_derivatives(false),
116      oes_egl_image_external(false),
117      oes_depth24(false),
118      oes_compressed_etc1_rgb8_texture(false),
119      packed_depth24_stencil8(false),
120      npot_ok(false),
121      enable_texture_float_linear(false),
122      enable_texture_half_float_linear(false),
123      angle_translated_shader_source(false),
124      angle_pack_reverse_row_order(false),
125      arb_texture_rectangle(false),
126      angle_instanced_arrays(false),
127      occlusion_query_boolean(false),
128      use_arb_occlusion_query2_for_occlusion_query_boolean(false),
129      use_arb_occlusion_query_for_occlusion_query_boolean(false),
130      native_vertex_array_object(false),
131      ext_texture_format_bgra8888(false),
132      enable_shader_name_hashing(false),
133      enable_samplers(false),
134      ext_draw_buffers(false),
135      ext_frag_depth(false),
136      ext_shader_texture_lod(false),
137      use_async_readpixels(false),
138      map_buffer_range(false),
139      ext_discard_framebuffer(false),
140      angle_depth_texture(false),
141      is_angle(false),
142      is_swiftshader(false),
143      angle_texture_usage(false),
144      ext_texture_storage(false),
145      chromium_path_rendering(false) {
146}
147
148FeatureInfo::Workarounds::Workarounds() :
149#define GPU_OP(type, name) name(false),
150    GPU_DRIVER_BUG_WORKAROUNDS(GPU_OP)
151#undef GPU_OP
152    max_texture_size(0),
153    max_cube_map_texture_size(0),
154    max_fragment_uniform_vectors(0),
155    max_varying_vectors(0),
156    max_vertex_uniform_vectors(0) {
157}
158
159FeatureInfo::FeatureInfo() {
160  InitializeBasicState(*CommandLine::ForCurrentProcess());
161}
162
163FeatureInfo::FeatureInfo(const CommandLine& command_line) {
164  InitializeBasicState(command_line);
165}
166
167void FeatureInfo::InitializeBasicState(const CommandLine& command_line) {
168  if (command_line.HasSwitch(switches::kGpuDriverBugWorkarounds)) {
169    std::string types = command_line.GetSwitchValueASCII(
170        switches::kGpuDriverBugWorkarounds);
171    StringToWorkarounds(types, &workarounds_);
172  }
173  feature_flags_.enable_shader_name_hashing =
174      !command_line.HasSwitch(switches::kDisableShaderNameHashing);
175
176  feature_flags_.is_swiftshader =
177      (command_line.GetSwitchValueASCII(switches::kUseGL) == "swiftshader");
178
179  static const GLenum kAlphaTypes[] = {
180      GL_UNSIGNED_BYTE,
181  };
182  static const GLenum kRGBTypes[] = {
183      GL_UNSIGNED_BYTE,
184      GL_UNSIGNED_SHORT_5_6_5,
185  };
186  static const GLenum kRGBATypes[] = {
187      GL_UNSIGNED_BYTE,
188      GL_UNSIGNED_SHORT_4_4_4_4,
189      GL_UNSIGNED_SHORT_5_5_5_1,
190  };
191  static const GLenum kLuminanceTypes[] = {
192      GL_UNSIGNED_BYTE,
193  };
194  static const GLenum kLuminanceAlphaTypes[] = {
195      GL_UNSIGNED_BYTE,
196  };
197  static const FormatInfo kFormatTypes[] = {
198    { GL_ALPHA, kAlphaTypes, arraysize(kAlphaTypes), },
199    { GL_RGB, kRGBTypes, arraysize(kRGBTypes), },
200    { GL_RGBA, kRGBATypes, arraysize(kRGBATypes), },
201    { GL_LUMINANCE, kLuminanceTypes, arraysize(kLuminanceTypes), },
202    { GL_LUMINANCE_ALPHA, kLuminanceAlphaTypes,
203      arraysize(kLuminanceAlphaTypes), } ,
204  };
205  for (size_t ii = 0; ii < arraysize(kFormatTypes); ++ii) {
206    const FormatInfo& info = kFormatTypes[ii];
207    ValueValidator<GLenum>& validator = texture_format_validators_[info.format];
208    for (size_t jj = 0; jj < info.count; ++jj) {
209      validator.AddValue(info.types[jj]);
210    }
211  }
212}
213
214bool FeatureInfo::Initialize() {
215  disallowed_features_ = DisallowedFeatures();
216  InitializeFeatures();
217  return true;
218}
219
220bool FeatureInfo::Initialize(const DisallowedFeatures& disallowed_features) {
221  disallowed_features_ = disallowed_features;
222  InitializeFeatures();
223  return true;
224}
225
226void FeatureInfo::InitializeFeatures() {
227  // Figure out what extensions to turn on.
228  StringSet extensions(
229      reinterpret_cast<const char*>(glGetString(GL_EXTENSIONS)));
230
231  const char* renderer_str =
232      reinterpret_cast<const char*>(glGetString(GL_RENDERER));
233  if (renderer_str) {
234    feature_flags_.is_angle = StartsWithASCII(renderer_str, "ANGLE", true);
235  }
236
237  bool is_es3 = false;
238  const char* version_str =
239      reinterpret_cast<const char*>(glGetString(GL_VERSION));
240  if (version_str) {
241    std::string lstr(base::StringToLowerASCII(std::string(version_str)));
242    is_es3 = (lstr.substr(0, 12) == "opengl es 3.");
243  }
244
245  AddExtensionString("GL_ANGLE_translated_shader_source");
246  AddExtensionString("GL_CHROMIUM_async_pixel_transfers");
247  AddExtensionString("GL_CHROMIUM_bind_uniform_location");
248  AddExtensionString("GL_CHROMIUM_command_buffer_query");
249  AddExtensionString("GL_CHROMIUM_command_buffer_latency_query");
250  AddExtensionString("GL_CHROMIUM_copy_texture");
251  AddExtensionString("GL_CHROMIUM_get_error_query");
252  AddExtensionString("GL_CHROMIUM_lose_context");
253  AddExtensionString("GL_CHROMIUM_pixel_transfer_buffer_object");
254  AddExtensionString("GL_CHROMIUM_rate_limit_offscreen_context");
255  AddExtensionString("GL_CHROMIUM_resize");
256  AddExtensionString("GL_CHROMIUM_resource_safe");
257  AddExtensionString("GL_CHROMIUM_strict_attribs");
258  AddExtensionString("GL_CHROMIUM_texture_mailbox");
259  AddExtensionString("GL_EXT_debug_marker");
260
261  // OES_vertex_array_object is emulated if not present natively,
262  // so the extension string is always exposed.
263  AddExtensionString("GL_OES_vertex_array_object");
264
265  if (!disallowed_features_.gpu_memory_manager)
266    AddExtensionString("GL_CHROMIUM_gpu_memory_manager");
267
268  if (extensions.Contains("GL_ANGLE_translated_shader_source")) {
269    feature_flags_.angle_translated_shader_source = true;
270  }
271
272  // Check if we should allow GL_EXT_texture_compression_dxt1 and
273  // GL_EXT_texture_compression_s3tc.
274  bool enable_dxt1 = false;
275  bool enable_dxt3 = false;
276  bool enable_dxt5 = false;
277  bool have_s3tc = extensions.Contains("GL_EXT_texture_compression_s3tc");
278  bool have_dxt3 =
279      have_s3tc || extensions.Contains("GL_ANGLE_texture_compression_dxt3");
280  bool have_dxt5 =
281      have_s3tc || extensions.Contains("GL_ANGLE_texture_compression_dxt5");
282
283  if (extensions.Contains("GL_EXT_texture_compression_dxt1") || have_s3tc) {
284    enable_dxt1 = true;
285  }
286  if (have_dxt3) {
287    enable_dxt3 = true;
288  }
289  if (have_dxt5) {
290    enable_dxt5 = true;
291  }
292
293  if (enable_dxt1) {
294    AddExtensionString("GL_EXT_texture_compression_dxt1");
295    validators_.compressed_texture_format.AddValue(
296        GL_COMPRESSED_RGB_S3TC_DXT1_EXT);
297    validators_.compressed_texture_format.AddValue(
298        GL_COMPRESSED_RGBA_S3TC_DXT1_EXT);
299  }
300
301  if (enable_dxt3) {
302    // The difference between GL_EXT_texture_compression_s3tc and
303    // GL_CHROMIUM_texture_compression_dxt3 is that the former
304    // requires on the fly compression. The latter does not.
305    AddExtensionString("GL_CHROMIUM_texture_compression_dxt3");
306    validators_.compressed_texture_format.AddValue(
307        GL_COMPRESSED_RGBA_S3TC_DXT3_EXT);
308  }
309
310  if (enable_dxt5) {
311    // The difference between GL_EXT_texture_compression_s3tc and
312    // GL_CHROMIUM_texture_compression_dxt5 is that the former
313    // requires on the fly compression. The latter does not.
314    AddExtensionString("GL_CHROMIUM_texture_compression_dxt5");
315    validators_.compressed_texture_format.AddValue(
316        GL_COMPRESSED_RGBA_S3TC_DXT5_EXT);
317  }
318
319  // Check if we should enable GL_EXT_texture_filter_anisotropic.
320  if (extensions.Contains("GL_EXT_texture_filter_anisotropic")) {
321    AddExtensionString("GL_EXT_texture_filter_anisotropic");
322    validators_.texture_parameter.AddValue(
323        GL_TEXTURE_MAX_ANISOTROPY_EXT);
324    validators_.g_l_state.AddValue(
325        GL_MAX_TEXTURE_MAX_ANISOTROPY_EXT);
326  }
327
328  // Check if we should support GL_OES_packed_depth_stencil and/or
329  // GL_GOOGLE_depth_texture / GL_CHROMIUM_depth_texture.
330  //
331  // NOTE: GL_OES_depth_texture requires support for depth cubemaps.
332  // GL_ARB_depth_texture requires other features that
333  // GL_OES_packed_depth_stencil does not provide.
334  //
335  // Therefore we made up GL_GOOGLE_depth_texture / GL_CHROMIUM_depth_texture.
336  //
337  // GL_GOOGLE_depth_texture is legacy. As we exposed it into NaCl we can't
338  // get rid of it.
339  //
340  bool enable_depth_texture = false;
341  if (!workarounds_.disable_depth_texture &&
342      (extensions.Contains("GL_ARB_depth_texture") ||
343       extensions.Contains("GL_OES_depth_texture") ||
344       extensions.Contains("GL_ANGLE_depth_texture") || is_es3)) {
345    enable_depth_texture = true;
346    feature_flags_.angle_depth_texture =
347        extensions.Contains("GL_ANGLE_depth_texture");
348  }
349
350  if (enable_depth_texture) {
351    AddExtensionString("GL_CHROMIUM_depth_texture");
352    AddExtensionString("GL_GOOGLE_depth_texture");
353    texture_format_validators_[GL_DEPTH_COMPONENT].AddValue(GL_UNSIGNED_SHORT);
354    texture_format_validators_[GL_DEPTH_COMPONENT].AddValue(GL_UNSIGNED_INT);
355    validators_.texture_internal_format.AddValue(GL_DEPTH_COMPONENT);
356    validators_.texture_format.AddValue(GL_DEPTH_COMPONENT);
357    validators_.pixel_type.AddValue(GL_UNSIGNED_SHORT);
358    validators_.pixel_type.AddValue(GL_UNSIGNED_INT);
359  }
360
361  if (extensions.Contains("GL_EXT_packed_depth_stencil") ||
362      extensions.Contains("GL_OES_packed_depth_stencil") || is_es3) {
363    AddExtensionString("GL_OES_packed_depth_stencil");
364    feature_flags_.packed_depth24_stencil8 = true;
365    if (enable_depth_texture) {
366      texture_format_validators_[GL_DEPTH_STENCIL]
367          .AddValue(GL_UNSIGNED_INT_24_8);
368      validators_.texture_internal_format.AddValue(GL_DEPTH_STENCIL);
369      validators_.texture_format.AddValue(GL_DEPTH_STENCIL);
370      validators_.pixel_type.AddValue(GL_UNSIGNED_INT_24_8);
371    }
372    validators_.render_buffer_format.AddValue(GL_DEPTH24_STENCIL8);
373  }
374
375  if (is_es3 || extensions.Contains("GL_OES_vertex_array_object") ||
376      extensions.Contains("GL_ARB_vertex_array_object") ||
377      extensions.Contains("GL_APPLE_vertex_array_object")) {
378    feature_flags_.native_vertex_array_object = true;
379  }
380
381  // If we're using client_side_arrays we have to emulate
382  // vertex array objects since vertex array objects do not work
383  // with client side arrays.
384  if (workarounds_.use_client_side_arrays_for_stream_buffers) {
385    feature_flags_.native_vertex_array_object = false;
386  }
387
388  if (is_es3 || extensions.Contains("GL_OES_element_index_uint") ||
389      gfx::HasDesktopGLFeatures()) {
390    AddExtensionString("GL_OES_element_index_uint");
391    validators_.index_type.AddValue(GL_UNSIGNED_INT);
392  }
393
394  bool enable_texture_format_bgra8888 = false;
395  bool enable_read_format_bgra = false;
396  bool enable_render_buffer_bgra = false;
397  bool enable_immutable_texture_format_bgra_on_es3 =
398      extensions.Contains("GL_APPLE_texture_format_BGRA8888");
399
400  // Check if we should allow GL_EXT_texture_format_BGRA8888
401  if (extensions.Contains("GL_EXT_texture_format_BGRA8888") ||
402      enable_immutable_texture_format_bgra_on_es3 ||
403      extensions.Contains("GL_EXT_bgra")) {
404    enable_texture_format_bgra8888 = true;
405  }
406
407  if (extensions.Contains("GL_EXT_bgra")) {
408    enable_render_buffer_bgra = true;
409  }
410
411  if (extensions.Contains("GL_EXT_read_format_bgra") ||
412      extensions.Contains("GL_EXT_bgra")) {
413    enable_read_format_bgra = true;
414  }
415
416  if (enable_texture_format_bgra8888) {
417    feature_flags_.ext_texture_format_bgra8888 = true;
418    AddExtensionString("GL_EXT_texture_format_BGRA8888");
419    texture_format_validators_[GL_BGRA_EXT].AddValue(GL_UNSIGNED_BYTE);
420    validators_.texture_internal_format.AddValue(GL_BGRA_EXT);
421    validators_.texture_format.AddValue(GL_BGRA_EXT);
422  }
423
424  if (enable_read_format_bgra) {
425    AddExtensionString("GL_EXT_read_format_bgra");
426    validators_.read_pixel_format.AddValue(GL_BGRA_EXT);
427  }
428
429  if (enable_render_buffer_bgra) {
430    AddExtensionString("GL_CHROMIUM_renderbuffer_format_BGRA8888");
431    validators_.render_buffer_format.AddValue(GL_BGRA8_EXT);
432  }
433
434  if (extensions.Contains("GL_OES_rgb8_rgba8") || gfx::HasDesktopGLFeatures()) {
435    AddExtensionString("GL_OES_rgb8_rgba8");
436    validators_.render_buffer_format.AddValue(GL_RGB8_OES);
437    validators_.render_buffer_format.AddValue(GL_RGBA8_OES);
438  }
439
440  // Check if we should allow GL_OES_texture_npot
441  if (is_es3 || extensions.Contains("GL_ARB_texture_non_power_of_two") ||
442      extensions.Contains("GL_OES_texture_npot")) {
443    AddExtensionString("GL_OES_texture_npot");
444    feature_flags_.npot_ok = true;
445  }
446
447  // Check if we should allow GL_OES_texture_float, GL_OES_texture_half_float,
448  // GL_OES_texture_float_linear, GL_OES_texture_half_float_linear
449  bool enable_texture_float = false;
450  bool enable_texture_float_linear = false;
451  bool enable_texture_half_float = false;
452  bool enable_texture_half_float_linear = false;
453
454  bool may_enable_chromium_color_buffer_float = false;
455
456  if (extensions.Contains("GL_ARB_texture_float")) {
457    enable_texture_float = true;
458    enable_texture_float_linear = true;
459    enable_texture_half_float = true;
460    enable_texture_half_float_linear = true;
461    may_enable_chromium_color_buffer_float = true;
462  } else {
463    if (is_es3 || extensions.Contains("GL_OES_texture_float")) {
464      enable_texture_float = true;
465      if (extensions.Contains("GL_OES_texture_float_linear")) {
466        enable_texture_float_linear = true;
467      }
468      if ((is_es3 && extensions.Contains("GL_EXT_color_buffer_float")) ||
469          feature_flags_.is_angle) {
470        may_enable_chromium_color_buffer_float = true;
471      }
472    }
473    // TODO(dshwang): GLES3 supports half float by default but GL_HALF_FLOAT_OES
474    // isn't equal to GL_HALF_FLOAT.
475    if (extensions.Contains("GL_OES_texture_half_float")) {
476      enable_texture_half_float = true;
477      if (extensions.Contains("GL_OES_texture_half_float_linear")) {
478        enable_texture_half_float_linear = true;
479      }
480    }
481  }
482
483  if (enable_texture_float) {
484    texture_format_validators_[GL_ALPHA].AddValue(GL_FLOAT);
485    texture_format_validators_[GL_RGB].AddValue(GL_FLOAT);
486    texture_format_validators_[GL_RGBA].AddValue(GL_FLOAT);
487    texture_format_validators_[GL_LUMINANCE].AddValue(GL_FLOAT);
488    texture_format_validators_[GL_LUMINANCE_ALPHA].AddValue(GL_FLOAT);
489    validators_.pixel_type.AddValue(GL_FLOAT);
490    validators_.read_pixel_type.AddValue(GL_FLOAT);
491    AddExtensionString("GL_OES_texture_float");
492    if (enable_texture_float_linear) {
493      AddExtensionString("GL_OES_texture_float_linear");
494    }
495  }
496
497  if (enable_texture_half_float) {
498    texture_format_validators_[GL_ALPHA].AddValue(GL_HALF_FLOAT_OES);
499    texture_format_validators_[GL_RGB].AddValue(GL_HALF_FLOAT_OES);
500    texture_format_validators_[GL_RGBA].AddValue(GL_HALF_FLOAT_OES);
501    texture_format_validators_[GL_LUMINANCE].AddValue(GL_HALF_FLOAT_OES);
502    texture_format_validators_[GL_LUMINANCE_ALPHA].AddValue(GL_HALF_FLOAT_OES);
503    validators_.pixel_type.AddValue(GL_HALF_FLOAT_OES);
504    validators_.read_pixel_type.AddValue(GL_HALF_FLOAT_OES);
505    AddExtensionString("GL_OES_texture_half_float");
506    if (enable_texture_half_float_linear) {
507      AddExtensionString("GL_OES_texture_half_float_linear");
508    }
509  }
510
511  if (may_enable_chromium_color_buffer_float) {
512    COMPILE_ASSERT(GL_RGBA32F_ARB == GL_RGBA32F &&
513                   GL_RGBA32F_EXT == GL_RGBA32F &&
514                   GL_RGB32F_ARB == GL_RGB32F &&
515                   GL_RGB32F_EXT == GL_RGB32F,
516                   sized_float_internal_format_variations_must_match);
517    // We don't check extension support beyond ARB_texture_float on desktop GL,
518    // and format support varies between GL configurations. For example, spec
519    // prior to OpenGL 3.0 mandates framebuffer support only for one
520    // implementation-chosen format, and ES3.0 EXT_color_buffer_float does not
521    // support rendering to RGB32F. Check for framebuffer completeness with
522    // formats that the extensions expose, and only enable an extension when a
523    // framebuffer created with its texture format is reported as complete.
524    GLint fb_binding = 0;
525    GLint tex_binding = 0;
526    glGetIntegerv(GL_FRAMEBUFFER_BINDING, &fb_binding);
527    glGetIntegerv(GL_TEXTURE_BINDING_2D, &tex_binding);
528
529    GLuint tex_id = 0;
530    GLuint fb_id = 0;
531    GLsizei width = 16;
532
533    glGenTextures(1, &tex_id);
534    glGenFramebuffersEXT(1, &fb_id);
535    glBindTexture(GL_TEXTURE_2D, tex_id);
536    // Nearest filter needed for framebuffer completeness on some drivers.
537    glTexParameteri(GL_TEXTURE_2D, GL_TEXTURE_MIN_FILTER, GL_NEAREST);
538    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGBA32F, width, width, 0, GL_RGBA,
539                 GL_FLOAT, NULL);
540    glBindFramebufferEXT(GL_FRAMEBUFFER, fb_id);
541    glFramebufferTexture2DEXT(GL_FRAMEBUFFER, GL_COLOR_ATTACHMENT0,
542                              GL_TEXTURE_2D, tex_id, 0);
543    GLenum statusRGBA = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
544    glTexImage2D(GL_TEXTURE_2D, 0, GL_RGB32F, width, width, 0, GL_RGB,
545                 GL_FLOAT, NULL);
546    GLenum statusRGB = glCheckFramebufferStatusEXT(GL_FRAMEBUFFER);
547    glDeleteFramebuffersEXT(1, &fb_id);
548    glDeleteTextures(1, &tex_id);
549
550    glBindFramebufferEXT(GL_FRAMEBUFFER, static_cast<GLuint>(fb_binding));
551    glBindTexture(GL_TEXTURE_2D, static_cast<GLuint>(tex_binding));
552
553    DCHECK(glGetError() == GL_NO_ERROR);
554
555    if (statusRGBA == GL_FRAMEBUFFER_COMPLETE) {
556      validators_.texture_internal_format.AddValue(GL_RGBA32F);
557      feature_flags_.chromium_color_buffer_float_rgba = true;
558      AddExtensionString("GL_CHROMIUM_color_buffer_float_rgba");
559    }
560    if (statusRGB == GL_FRAMEBUFFER_COMPLETE) {
561      validators_.texture_internal_format.AddValue(GL_RGB32F);
562      feature_flags_.chromium_color_buffer_float_rgb = true;
563      AddExtensionString("GL_CHROMIUM_color_buffer_float_rgb");
564    }
565  }
566
567  // Check for multisample support
568  if (!workarounds_.disable_multisampling) {
569    bool ext_has_multisample =
570        extensions.Contains("GL_EXT_framebuffer_multisample") || is_es3;
571    if (feature_flags_.is_angle) {
572      ext_has_multisample |=
573          extensions.Contains("GL_ANGLE_framebuffer_multisample");
574    }
575    feature_flags_.use_core_framebuffer_multisample = is_es3;
576    if (ext_has_multisample) {
577      feature_flags_.chromium_framebuffer_multisample = true;
578      validators_.frame_buffer_target.AddValue(GL_READ_FRAMEBUFFER_EXT);
579      validators_.frame_buffer_target.AddValue(GL_DRAW_FRAMEBUFFER_EXT);
580      validators_.g_l_state.AddValue(GL_READ_FRAMEBUFFER_BINDING_EXT);
581      validators_.g_l_state.AddValue(GL_MAX_SAMPLES_EXT);
582      validators_.render_buffer_parameter.AddValue(GL_RENDERBUFFER_SAMPLES_EXT);
583      AddExtensionString("GL_CHROMIUM_framebuffer_multisample");
584    }
585    if (extensions.Contains("GL_EXT_multisampled_render_to_texture")) {
586      feature_flags_.multisampled_render_to_texture = true;
587    } else if (extensions.Contains("GL_IMG_multisampled_render_to_texture")) {
588      feature_flags_.multisampled_render_to_texture = true;
589      feature_flags_.use_img_for_multisampled_render_to_texture = true;
590    }
591    if (feature_flags_.multisampled_render_to_texture) {
592      validators_.render_buffer_parameter.AddValue(
593          GL_RENDERBUFFER_SAMPLES_EXT);
594      validators_.g_l_state.AddValue(GL_MAX_SAMPLES_EXT);
595      validators_.frame_buffer_parameter.AddValue(
596          GL_FRAMEBUFFER_ATTACHMENT_TEXTURE_SAMPLES_EXT);
597      AddExtensionString("GL_EXT_multisampled_render_to_texture");
598    }
599  }
600
601  if (extensions.Contains("GL_OES_depth24") || gfx::HasDesktopGLFeatures() ||
602      is_es3) {
603    AddExtensionString("GL_OES_depth24");
604    feature_flags_.oes_depth24 = true;
605    validators_.render_buffer_format.AddValue(GL_DEPTH_COMPONENT24);
606  }
607
608  if (!workarounds_.disable_oes_standard_derivatives &&
609      (is_es3 || extensions.Contains("GL_OES_standard_derivatives") ||
610       gfx::HasDesktopGLFeatures())) {
611    AddExtensionString("GL_OES_standard_derivatives");
612    feature_flags_.oes_standard_derivatives = true;
613    validators_.hint_target.AddValue(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES);
614    validators_.g_l_state.AddValue(GL_FRAGMENT_SHADER_DERIVATIVE_HINT_OES);
615  }
616
617  if (extensions.Contains("GL_OES_EGL_image_external")) {
618    AddExtensionString("GL_OES_EGL_image_external");
619    feature_flags_.oes_egl_image_external = true;
620    validators_.texture_bind_target.AddValue(GL_TEXTURE_EXTERNAL_OES);
621    validators_.get_tex_param_target.AddValue(GL_TEXTURE_EXTERNAL_OES);
622    validators_.texture_parameter.AddValue(GL_REQUIRED_TEXTURE_IMAGE_UNITS_OES);
623    validators_.g_l_state.AddValue(GL_TEXTURE_BINDING_EXTERNAL_OES);
624  }
625
626  if (extensions.Contains("GL_OES_compressed_ETC1_RGB8_texture")) {
627    AddExtensionString("GL_OES_compressed_ETC1_RGB8_texture");
628    feature_flags_.oes_compressed_etc1_rgb8_texture = true;
629    validators_.compressed_texture_format.AddValue(GL_ETC1_RGB8_OES);
630  }
631
632  if (extensions.Contains("GL_AMD_compressed_ATC_texture")) {
633    AddExtensionString("GL_AMD_compressed_ATC_texture");
634    validators_.compressed_texture_format.AddValue(
635        GL_ATC_RGB_AMD);
636    validators_.compressed_texture_format.AddValue(
637        GL_ATC_RGBA_EXPLICIT_ALPHA_AMD);
638    validators_.compressed_texture_format.AddValue(
639        GL_ATC_RGBA_INTERPOLATED_ALPHA_AMD);
640  }
641
642  if (extensions.Contains("GL_IMG_texture_compression_pvrtc")) {
643    AddExtensionString("GL_IMG_texture_compression_pvrtc");
644    validators_.compressed_texture_format.AddValue(
645        GL_COMPRESSED_RGB_PVRTC_4BPPV1_IMG);
646    validators_.compressed_texture_format.AddValue(
647        GL_COMPRESSED_RGB_PVRTC_2BPPV1_IMG);
648    validators_.compressed_texture_format.AddValue(
649        GL_COMPRESSED_RGBA_PVRTC_4BPPV1_IMG);
650    validators_.compressed_texture_format.AddValue(
651        GL_COMPRESSED_RGBA_PVRTC_2BPPV1_IMG);
652  }
653
654  // Ideally we would only expose this extension on Mac OS X, to
655  // support GL_CHROMIUM_iosurface and the compositor. We don't want
656  // applications to start using it; they should use ordinary non-
657  // power-of-two textures. However, for unit testing purposes we
658  // expose it on all supported platforms.
659  if (extensions.Contains("GL_ARB_texture_rectangle")) {
660    AddExtensionString("GL_ARB_texture_rectangle");
661    feature_flags_.arb_texture_rectangle = true;
662    validators_.texture_bind_target.AddValue(GL_TEXTURE_RECTANGLE_ARB);
663    // For the moment we don't add this enum to the texture_target
664    // validator. This implies that the only way to get image data into a
665    // rectangular texture is via glTexImageIOSurface2DCHROMIUM, which is
666    // just fine since again we don't want applications depending on this
667    // extension.
668    validators_.get_tex_param_target.AddValue(GL_TEXTURE_RECTANGLE_ARB);
669    validators_.g_l_state.AddValue(GL_TEXTURE_BINDING_RECTANGLE_ARB);
670  }
671
672#if defined(OS_MACOSX)
673  AddExtensionString("GL_CHROMIUM_iosurface");
674#endif
675
676  // TODO(gman): Add support for these extensions.
677  //     GL_OES_depth32
678
679  feature_flags_.enable_texture_float_linear |= enable_texture_float_linear;
680  feature_flags_.enable_texture_half_float_linear |=
681      enable_texture_half_float_linear;
682
683  if (extensions.Contains("GL_ANGLE_pack_reverse_row_order")) {
684    AddExtensionString("GL_ANGLE_pack_reverse_row_order");
685    feature_flags_.angle_pack_reverse_row_order = true;
686    validators_.pixel_store.AddValue(GL_PACK_REVERSE_ROW_ORDER_ANGLE);
687    validators_.g_l_state.AddValue(GL_PACK_REVERSE_ROW_ORDER_ANGLE);
688  }
689
690  if (extensions.Contains("GL_ANGLE_texture_usage")) {
691    feature_flags_.angle_texture_usage = true;
692    AddExtensionString("GL_ANGLE_texture_usage");
693    validators_.texture_parameter.AddValue(GL_TEXTURE_USAGE_ANGLE);
694  }
695
696  // Note: Only APPLE_texture_format_BGRA8888 extension allows BGRA8_EXT in
697  // ES3's glTexStorage2D. We prefer support BGRA to texture storage.
698  // So we don't expose GL_EXT_texture_storage when ES3 +
699  // GL_EXT_texture_format_BGRA8888 because we fail the GL_BGRA8 requirement.
700  // However we expose GL_EXT_texture_storage when just ES3 because we don't
701  // claim to handle GL_BGRA8.
702  bool support_texture_storage_on_es3 =
703      (is_es3 && enable_immutable_texture_format_bgra_on_es3) ||
704      (is_es3 && !enable_texture_format_bgra8888);
705  if (extensions.Contains("GL_EXT_texture_storage") ||
706      extensions.Contains("GL_ARB_texture_storage") ||
707      support_texture_storage_on_es3) {
708    feature_flags_.ext_texture_storage = true;
709    AddExtensionString("GL_EXT_texture_storage");
710    validators_.texture_parameter.AddValue(GL_TEXTURE_IMMUTABLE_FORMAT_EXT);
711    if (enable_texture_format_bgra8888)
712        validators_.texture_internal_format_storage.AddValue(GL_BGRA8_EXT);
713    if (enable_texture_float) {
714        validators_.texture_internal_format_storage.AddValue(GL_RGBA32F_EXT);
715        validators_.texture_internal_format_storage.AddValue(GL_RGB32F_EXT);
716        validators_.texture_internal_format_storage.AddValue(GL_ALPHA32F_EXT);
717        validators_.texture_internal_format_storage.AddValue(
718            GL_LUMINANCE32F_EXT);
719        validators_.texture_internal_format_storage.AddValue(
720            GL_LUMINANCE_ALPHA32F_EXT);
721    }
722    if (enable_texture_half_float) {
723        validators_.texture_internal_format_storage.AddValue(GL_RGBA16F_EXT);
724        validators_.texture_internal_format_storage.AddValue(GL_RGB16F_EXT);
725        validators_.texture_internal_format_storage.AddValue(GL_ALPHA16F_EXT);
726        validators_.texture_internal_format_storage.AddValue(
727            GL_LUMINANCE16F_EXT);
728        validators_.texture_internal_format_storage.AddValue(
729            GL_LUMINANCE_ALPHA16F_EXT);
730    }
731  }
732
733  bool have_ext_occlusion_query_boolean =
734      extensions.Contains("GL_EXT_occlusion_query_boolean");
735  bool have_arb_occlusion_query2 =
736      extensions.Contains("GL_ARB_occlusion_query2");
737  bool have_arb_occlusion_query =
738      extensions.Contains("GL_ARB_occlusion_query");
739
740  if (!workarounds_.disable_ext_occlusion_query &&
741      (have_ext_occlusion_query_boolean ||
742       have_arb_occlusion_query2 ||
743       have_arb_occlusion_query)) {
744    AddExtensionString("GL_EXT_occlusion_query_boolean");
745    feature_flags_.occlusion_query_boolean = true;
746    feature_flags_.use_arb_occlusion_query2_for_occlusion_query_boolean =
747        !have_ext_occlusion_query_boolean && have_arb_occlusion_query2;
748    feature_flags_.use_arb_occlusion_query_for_occlusion_query_boolean =
749        !have_ext_occlusion_query_boolean && have_arb_occlusion_query &&
750        !have_arb_occlusion_query2;
751  }
752
753  if (!workarounds_.disable_angle_instanced_arrays &&
754      (extensions.Contains("GL_ANGLE_instanced_arrays") ||
755       (extensions.Contains("GL_ARB_instanced_arrays") &&
756        extensions.Contains("GL_ARB_draw_instanced")) ||
757       is_es3)) {
758    AddExtensionString("GL_ANGLE_instanced_arrays");
759    feature_flags_.angle_instanced_arrays = true;
760    validators_.vertex_attribute.AddValue(GL_VERTEX_ATTRIB_ARRAY_DIVISOR_ANGLE);
761  }
762
763  if (!workarounds_.disable_ext_draw_buffers &&
764      (extensions.Contains("GL_ARB_draw_buffers") ||
765       extensions.Contains("GL_EXT_draw_buffers"))) {
766    AddExtensionString("GL_EXT_draw_buffers");
767    feature_flags_.ext_draw_buffers = true;
768
769    GLint max_color_attachments = 0;
770    glGetIntegerv(GL_MAX_COLOR_ATTACHMENTS_EXT, &max_color_attachments);
771    for (GLenum i = GL_COLOR_ATTACHMENT1_EXT;
772         i < static_cast<GLenum>(GL_COLOR_ATTACHMENT0 + max_color_attachments);
773         ++i) {
774      validators_.attachment.AddValue(i);
775    }
776    COMPILE_ASSERT(GL_COLOR_ATTACHMENT0_EXT == GL_COLOR_ATTACHMENT0,
777                   color_attachment0_variation_must_match);
778
779    validators_.g_l_state.AddValue(GL_MAX_COLOR_ATTACHMENTS_EXT);
780    validators_.g_l_state.AddValue(GL_MAX_DRAW_BUFFERS_ARB);
781    GLint max_draw_buffers = 0;
782    glGetIntegerv(GL_MAX_DRAW_BUFFERS_ARB, &max_draw_buffers);
783    for (GLenum i = GL_DRAW_BUFFER0_ARB;
784         i < static_cast<GLenum>(GL_DRAW_BUFFER0_ARB + max_draw_buffers);
785         ++i) {
786      validators_.g_l_state.AddValue(i);
787    }
788  }
789
790  if (is_es3 || extensions.Contains("GL_EXT_blend_minmax") ||
791      gfx::HasDesktopGLFeatures()) {
792    AddExtensionString("GL_EXT_blend_minmax");
793    validators_.equation.AddValue(GL_MIN_EXT);
794    validators_.equation.AddValue(GL_MAX_EXT);
795    COMPILE_ASSERT(GL_MIN_EXT == GL_MIN && GL_MAX_EXT == GL_MAX,
796                   min_max_variations_must_match);
797  }
798
799  // TODO(dshwang): GLES3 supports gl_FragDepth, not gl_FragDepthEXT.
800  if (extensions.Contains("GL_EXT_frag_depth") || gfx::HasDesktopGLFeatures()) {
801    AddExtensionString("GL_EXT_frag_depth");
802    feature_flags_.ext_frag_depth = true;
803  }
804
805  if (extensions.Contains("GL_EXT_shader_texture_lod") ||
806      gfx::HasDesktopGLFeatures()) {
807    AddExtensionString("GL_EXT_shader_texture_lod");
808    feature_flags_.ext_shader_texture_lod = true;
809  }
810
811#if !defined(OS_MACOSX)
812  if (workarounds_.disable_egl_khr_fence_sync) {
813    gfx::g_driver_egl.ext.b_EGL_KHR_fence_sync = false;
814  }
815  if (workarounds_.disable_egl_khr_wait_sync) {
816    gfx::g_driver_egl.ext.b_EGL_KHR_wait_sync = false;
817  }
818#endif
819  if (workarounds_.disable_arb_sync)
820    gfx::g_driver_gl.ext.b_GL_ARB_sync = false;
821  bool ui_gl_fence_works = gfx::GLFence::IsSupported();
822  UMA_HISTOGRAM_BOOLEAN("GPU.FenceSupport", ui_gl_fence_works);
823
824  feature_flags_.map_buffer_range =
825      is_es3 || extensions.Contains("GL_ARB_map_buffer_range");
826
827  // Really it's part of core OpenGL 2.1 and up, but let's assume the
828  // extension is still advertised.
829  bool has_pixel_buffers =
830      is_es3 || extensions.Contains("GL_ARB_pixel_buffer_object");
831
832  // We will use either glMapBuffer() or glMapBufferRange() for async readbacks.
833  if (has_pixel_buffers && ui_gl_fence_works &&
834      !workarounds_.disable_async_readpixels) {
835    feature_flags_.use_async_readpixels = true;
836  }
837
838  if (is_es3 || extensions.Contains("GL_ARB_sampler_objects")) {
839    feature_flags_.enable_samplers = true;
840    // TODO(dsinclair): Add AddExtensionString("GL_CHROMIUM_sampler_objects")
841    // when available.
842  }
843
844  if ((is_es3 || extensions.Contains("GL_EXT_discard_framebuffer")) &&
845      !workarounds_.disable_ext_discard_framebuffer) {
846    // DiscardFramebufferEXT is automatically bound to InvalidateFramebuffer.
847    AddExtensionString("GL_EXT_discard_framebuffer");
848    feature_flags_.ext_discard_framebuffer = true;
849  }
850
851  if (ui_gl_fence_works) {
852    AddExtensionString("GL_CHROMIUM_sync_query");
853    feature_flags_.chromium_sync_query = true;
854  }
855
856  if (extensions.Contains("GL_NV_path_rendering")) {
857    if (extensions.Contains("GL_EXT_direct_state_access") || is_es3) {
858      AddExtensionString("GL_CHROMIUM_path_rendering");
859      feature_flags_.chromium_path_rendering = true;
860      validators_.g_l_state.AddValue(GL_PATH_MODELVIEW_MATRIX_CHROMIUM);
861      validators_.g_l_state.AddValue(GL_PATH_PROJECTION_MATRIX_CHROMIUM);
862    }
863  }
864}
865
866void FeatureInfo::AddExtensionString(const char* s) {
867  std::string str(s);
868  size_t pos = extensions_.find(str);
869  while (pos != std::string::npos &&
870         pos + str.length() < extensions_.length() &&
871         extensions_.substr(pos + str.length(), 1) != " ") {
872    // This extension name is a substring of another.
873    pos = extensions_.find(str, pos + str.length());
874  }
875  if (pos == std::string::npos) {
876    extensions_ += (extensions_.empty() ? "" : " ") + str;
877  }
878}
879
880FeatureInfo::~FeatureInfo() {
881}
882
883}  // namespace gles2
884}  // namespace gpu
885