<commit_before>/**
 * Copyright 2010 The PlayN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package playn.html;

import com.google.gwt.dom.client.CanvasElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.webgl.client.WebGLContextAttributes;
import com.google.gwt.webgl.client.WebGLRenderingContext;

import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.ImmediateLayer;
import playn.core.SurfaceImage;
import playn.core.SurfaceLayer;
import playn.core.gl.GL20;
import playn.core.gl.GroupLayerGL;
import playn.core.gl.ImageLayerGL;
import playn.core.gl.ImmediateLayerGL;
import playn.core.gl.Scale;
import playn.core.gl.SurfaceGL;
import playn.core.gl.SurfaceImageGL;
import playn.core.gl.SurfaceLayerGL;

class HtmlGraphicsGL extends HtmlGraphics {

  private final CanvasElement canvas;
  private final HtmlGLContext ctx;
  private final GroupLayerGL rootLayer;

  HtmlGraphicsGL(HtmlPlatform platform, HtmlPlatform.Config config) throws RuntimeException {
    super(config);
    canvas = Document.get().createCanvasElement();
    canvas.setWidth(rootElement.getOffsetWidth());
    canvas.setHeight(rootElement.getOffsetHeight());
    rootElement.appendChild(canvas);

    try {
      WebGLContextAttributes attrs = WebGLContextAttributes.create();
      attrs.setAlpha(config.transparentCanvas);
      attrs.setAntialias(config.antiAliasing);

      // if this returns null, the browser doesn't support WebGL on this machine
      WebGLRenderingContext gl = WebGLRenderingContext.getContext(canvas, attrs);
      // Some systems seem to have a problem where they return a valid context, but it's in an
      // error state initially. We give up and fall back to Canvas in this case, because nothing
      // seems to work properly.
      if (gl == null || gl.getError() != WebGLRenderingContext.NO_ERROR) {
        throw new RuntimeException("GL context not created [err=" +
                                   (gl == null ? "null" : gl.getError()) + "]");
      }

      ctx = new HtmlGLContext(platform, config.scaleFactor, gl, canvas);
      rootLayer = new GroupLayerGL(ctx);
    } catch (RuntimeException re) {
      // Give up. HtmlPlatform will catch the exception and fall back to dom/canvas.
      rootElement.removeChild(canvas);
      throw re;
    }
  }

  @Override
  public void setSize(int width, int height) {
    super.setSize(width, height);
    canvas.setWidth(width);
    canvas.setHeight(height);
    ctx.setSize(width, height);
  }

  @Override<commit_msg>Restructure error checking to avoid reporting bogus error.<commit_after>/**
 * Copyright 2010 The PlayN Authors
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except
 * in compliance with the License. You may obtain a copy of the License at
 *
 * http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License
 * is distributed on an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express
 * or implied. See the License for the specific language governing permissions and limitations under
 * the License.
 */
package playn.html;

import com.google.gwt.dom.client.CanvasElement;
import com.google.gwt.dom.client.Document;
import com.google.gwt.dom.client.Element;
import com.google.gwt.webgl.client.WebGLContextAttributes;
import com.google.gwt.webgl.client.WebGLRenderingContext;

import playn.core.GroupLayer;
import playn.core.Image;
import playn.core.ImageLayer;
import playn.core.ImmediateLayer;
import playn.core.SurfaceImage;
import playn.core.SurfaceLayer;
import playn.core.gl.GL20;
import playn.core.gl.GroupLayerGL;
import playn.core.gl.ImageLayerGL;
import playn.core.gl.ImmediateLayerGL;
import playn.core.gl.Scale;
import playn.core.gl.SurfaceGL;
import playn.core.gl.SurfaceImageGL;
import playn.core.gl.SurfaceLayerGL;

class HtmlGraphicsGL extends HtmlGraphics {

  private final CanvasElement canvas;
  private final HtmlGLContext ctx;
  private final GroupLayerGL rootLayer;

  HtmlGraphicsGL(HtmlPlatform platform, HtmlPlatform.Config config) throws RuntimeException {
    super(config);
    canvas = Document.get().createCanvasElement();
    canvas.setWidth(rootElement.getOffsetWidth());
    canvas.setHeight(rootElement.getOffsetHeight());
    rootElement.appendChild(canvas);

    try {
      WebGLContextAttributes attrs = WebGLContextAttributes.create();
      attrs.setAlpha(config.transparentCanvas);
      attrs.setAntialias(config.antiAliasing);

      // if this returns null, the browser doesn't support WebGL on this machine
      WebGLRenderingContext gl = WebGLRenderingContext.getContext(canvas, attrs);
      if (gl == null)
        throw new RuntimeException("Unable to create GL context");

      // Some systems seem to have a problem where they return a non-null context, but it's in an
      // error state initially. We give up and fall back to Canvas in this case, because nothing
      // seems to work properly.
      int error = gl.getError();
      if (error != WebGLRenderingContext.NO_ERROR)
        throw new RuntimeException("GL context started with errors [err=" + error + "]");

      ctx = new HtmlGLContext(platform, config.scaleFactor, gl, canvas);
      rootLayer = new GroupLayerGL(ctx);

    } catch (RuntimeException re) {
      // Give up. HtmlPlatform will catch the exception and fall back to dom/canvas.
      rootElement.removeChild(canvas);
      throw re;
    }
  }

  @Override
  public void setSize(int width, int height) {
    super.setSize(width, height);
    canvas.setWidth(width);
    canvas.setHeight(height);
    ctx.setSize(width, height);
  }

  @Override
{'fraction_non_alphanumeric': 0.0915011914217633, 'fraction_numerical': 0.003177124702144559, 'mean_word_length': 3.9110764430577225, 'pattern_counts': {'":': 0, '<': 3, '<?xml version=': 0, '>': 3, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 1, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '7377363', 'n_tokens_mistral': 1782, 'n_tokens_neox': 1759, 'n_words': 551}