Android Open Source - android-plotter Double Buffer Mesh






From Project

Back to project page android-plotter.

License

The source code is released under:

Apache License

If you think the Android project android-plotter listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

package org.solovyev.android.plotter.meshes;
/*from w w w .j a va  2s . c o  m*/
import android.util.Log;
import org.solovyev.android.plotter.MeshConfig;

import javax.annotation.Nonnull;
import javax.annotation.Nullable;
import javax.annotation.concurrent.GuardedBy;
import javax.annotation.concurrent.ThreadSafe;
import javax.microedition.khronos.opengles.GL11;

@ThreadSafe
public class DoubleBufferMesh<M extends Mesh> implements Mesh {

  @Nonnull
  private static final String TAG = Meshes.getTag("DoubleBufferMesh");

  public static interface Swapper<M> {
    void swap(@Nonnull M current, @Nonnull M next);
  }

  @Nonnull
  private final Object lock = new Object();

  @GuardedBy("lock")
  private M current;

  @GuardedBy("lock")
  private M next;

  @Nonnull
  private final M first;

  @Nonnull
  private final M second;

  @Nullable
  private final Swapper<? super M> swapper;

  private DoubleBufferMesh(@Nonnull M first, @Nonnull M second, @Nullable Swapper<? super M> swapper) {
    this.first = first;
    this.second = second;
    this.swapper = swapper;
  }

  @Nonnull
  public static <M extends Mesh> DoubleBufferMesh<M> wrap(@Nonnull M mesh, @Nullable Swapper<? super M> swapper) {
    return new DoubleBufferMesh<M>(mesh, (M) mesh.copy(), swapper);
  }

  @Override
  public boolean init() {
    final M next = getNext();
    final boolean initialized = next.init();
    if (initialized) {
      Log.d(TAG, "Initializing next=" + next);
    }
    return initialized;
  }

  @Override
  public boolean initGl(@Nonnull GL11 gl, @Nonnull MeshConfig config) {
    final M next = getNext();
    final boolean initGl = next.initGl(gl, config);
    if (initGl) {
      swap(next);
      return false;
    }

    // initGl must be called for current mesh also as GL instance might have changed
    return getOther(next).initGl(gl, config);
  }

  private void swap(@Nonnull M next) {
    synchronized (lock) {
      Log.d(TAG, "Swapping current=" + getMeshName(this.current) + " with next=" + getMeshName(next));
      if (this.current == null) {
        this.next = this.second;
      } else {
        this.next = this.current;
      }
      this.current = next;
      if (swapper != null) {
        swapper.swap(current, this.next);
      }
    }
  }

  @Nonnull
  private String getMeshName(@Nonnull M mesh) {
    return mesh + "(" + (mesh == this.first ? 0 : 1) + ")";
  }

  @Nonnull
  public M getNext() {
    M next;
    synchronized (lock) {
      next = this.next != null ? this.next : this.first;
    }
    return next;
  }

  @Nonnull
  public M getFirst() {
    return first;
  }

  @Nonnull
  public M getSecond() {
    return second;
  }

  @Nonnull
  public M getOther(@Nonnull M mesh) {
    return this.first == mesh ? this.second : this.first;
  }

  @Override
  public void draw(@Nonnull GL11 gl) {
    final M current;
    synchronized (lock) {
      current = this.current;
    }

    if (current != null) {
      current.draw(gl);
    }
  }

  @Nonnull
  @Override
  public M copy() {
    throw new UnsupportedOperationException();
  }

  @Nonnull
  @Override
  public State getState() {
    throw new UnsupportedOperationException();
  }
}




Java Source Code List

com.android.texample.GLText.java
com.android.texample.SpriteBatch.java
com.android.texample.TexampleRenderer.java
com.android.texample.TextureRegion.java
com.android.texample.Vertices.java
org.solovyev.android.plotter.Angle.java
org.solovyev.android.plotter.AxisStyle.java
org.solovyev.android.plotter.Check.java
org.solovyev.android.plotter.Color.java
org.solovyev.android.plotter.DefaultPlotter.java
org.solovyev.android.plotter.Dimensions.java
org.solovyev.android.plotter.Frustum.java
org.solovyev.android.plotter.Function0.java
org.solovyev.android.plotter.Function1.java
org.solovyev.android.plotter.Function2.java
org.solovyev.android.plotter.Function.java
org.solovyev.android.plotter.LineStyle.java
org.solovyev.android.plotter.MeshConfig.java
org.solovyev.android.plotter.MultisampleConfigChooser.java
org.solovyev.android.plotter.PinchZoomTracker.java
org.solovyev.android.plotter.PlotData.java
org.solovyev.android.plotter.PlotFunction.java
org.solovyev.android.plotter.PlotRenderer.java
org.solovyev.android.plotter.PlotView.java
org.solovyev.android.plotter.Plot.java
org.solovyev.android.plotter.Plotter.java
org.solovyev.android.plotter.PlottingView.java
org.solovyev.android.plotter.Spf.java
org.solovyev.android.plotter.SuperFunction.java
org.solovyev.android.plotter.TouchHandler.java
org.solovyev.android.plotter.ZoomLevels.java
org.solovyev.android.plotter.Zoomer.java
org.solovyev.android.plotter.app.MainActivity.java
org.solovyev.android.plotter.app.PlotterApplication.java
org.solovyev.android.plotter.meshes.Arrays.java
org.solovyev.android.plotter.meshes.AxisGrid.java
org.solovyev.android.plotter.meshes.Axis.java
org.solovyev.android.plotter.meshes.BaseCube.java
org.solovyev.android.plotter.meshes.BaseCurve.java
org.solovyev.android.plotter.meshes.BaseMesh.java
org.solovyev.android.plotter.meshes.BaseSurface.java
org.solovyev.android.plotter.meshes.DimensionsAwareSwapper.java
org.solovyev.android.plotter.meshes.DimensionsAware.java
org.solovyev.android.plotter.meshes.DoubleBufferGroup.java
org.solovyev.android.plotter.meshes.DoubleBufferMesh.java
org.solovyev.android.plotter.meshes.FunctionGraph2d.java
org.solovyev.android.plotter.meshes.FunctionGraph3d.java
org.solovyev.android.plotter.meshes.FunctionGraphSwapper.java
org.solovyev.android.plotter.meshes.FunctionGraph.java
org.solovyev.android.plotter.meshes.Graph.java
org.solovyev.android.plotter.meshes.Group.java
org.solovyev.android.plotter.meshes.IndicesOrder.java
org.solovyev.android.plotter.meshes.ListGroup.java
org.solovyev.android.plotter.meshes.ListPool.java
org.solovyev.android.plotter.meshes.Mesh.java
org.solovyev.android.plotter.meshes.Meshes.java
org.solovyev.android.plotter.meshes.Pool.java
org.solovyev.android.plotter.meshes.Scene.java
org.solovyev.android.plotter.meshes.SolidCube.java
org.solovyev.android.plotter.meshes.SurfaceInitializer.java
org.solovyev.android.plotter.meshes.WireFrameCube.java