Android Open Source - Aviary-Android-SDK Aviary G L Surface View






From Project

Back to project page Aviary-Android-SDK.

License

The source code is released under:

AVIARY API TERMS OF USE Full Legal Agreement The following terms and conditions and the terms and conditions at http://www.aviary.com/terms (collectively, the ?Terms??) govern your use of any and ...

If you think the Android project Aviary-Android-SDK 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 com.aviary.android.feather.opengl;
//from w w  w .  ja va 2  s.  c om
import java.lang.ref.WeakReference;

import javax.microedition.khronos.egl.EGLConfig;
import javax.microedition.khronos.opengles.GL10;

import android.content.Context;
import android.graphics.Bitmap;
import android.opengl.GLSurfaceView;
import android.os.Handler;
import android.util.AttributeSet;
import android.util.Log;

import com.aviary.android.feather.common.log.LoggerFactory;
import com.aviary.android.feather.common.log.LoggerFactory.Logger;
import com.aviary.android.feather.common.log.LoggerFactory.LoggerType;
import com.aviary.android.feather.common.threading.Future;
import com.aviary.android.feather.common.threading.FutureListener;
import com.aviary.android.feather.common.threading.ThreadPool.Job;
import com.aviary.android.feather.common.threading.ThreadPool.Worker;

public class AviaryGLSurfaceView extends GLSurfaceView {

  private static final Logger logger = LoggerFactory.getLogger( "gl-surface", LoggerType.ConsoleLoggerType );

  private static final Handler mUIHandler = new Handler();

  public AviaryGLSurfaceView ( Context context ) {
    super( context );
    mPtr = init( context, null );
  }

  public AviaryGLSurfaceView ( Context context, AttributeSet attrs ) {
    super( context, attrs );
    mPtr = init( context, attrs );
  }

  private long init( Context context, AttributeSet attrs ) {
    this.setEGLContextClientVersion( 2 );
    this.setEGLConfigChooser( 8, 8, 8, 8, 0, 0 );
    this.setRenderer( new AviaryGLRenderer() );
    this.setRenderMode( RENDERMODE_WHEN_DIRTY );
    return nativeCreate();
  }

  @Override
  protected void onDetachedFromWindow() {
    logger.info( "onDetachedfromWindow" );
    nativeDispose();
    super.onDetachedFromWindow();
  }

  private void initializeOpenGL() {
    InitializeOpenGLJob job = new InitializeOpenGLJob();
    FutureListener<Void> listener = new FutureListener<Void>() {

      @Override
      public void onFutureDone( Future<Void> arg0 ) {
        fireOnSurfaceCreated();
      }
    };
    submit( job, listener );
  }

  private void setRenderbufferSize( final boolean changed, final int width, final int height ) {
    SetRenderbufferSizeJob job = new SetRenderbufferSizeJob();
    FutureListener<Void> listener = new FutureListener<Void>() {

      @Override
      public void onFutureDone( Future<Void> arg0 ) {
        fireOnSurfaceChanged( changed, width, height );
      }
    };
    submit( job, listener, width, height );
  }

  public Future<Boolean> executeEffect( String effectName, Bitmap input, boolean use_gpu, FutureListener<Boolean> listener ) {
    RenderJob job = new RenderJob( input );
    return submit( job, listener, effectName );
  }

  public Future<Boolean> writeBitmap( Bitmap output, FutureListener<Boolean> listener ) {
    WriteBitmapJob job = new WriteBitmapJob( output );
    return submit( job, listener );
  }

  /**
   * Returns the native pointer to the MoaGLSurfaceView
   * 
   * @return
   */
  private long nativeCreate() {
    logger.log( "nativeCreate" );
    synchronized ( mNativeLock ) {
      return n_create();
    }
  }

  /**
   * Free the native instance
   */
  private void nativeDispose() {
    logger.log( "nativeDispose" );
    synchronized ( mNativeLock ) {
      n_dispose( mPtr );
    }
  }

  /**
   * Initialize the GL data in the native instance
   * 
   * @return
   */
  private boolean nativeInitialize() {
    logger.log( "nativeInitialize" );
    synchronized ( mNativeLock ) {
      return n_initialize( mPtr );
    }
  }

  /**
   * Change the render buffer size in the native instance
   * 
   * @param width
   * @param height
   * @return
   */
  private boolean nativeSetRenderbufferSize( int width, int height ) {
    logger.log( "nativeSetRenderBufferSize" );
    synchronized ( mNativeLock ) {
      return n_setRenderbufferSize( mPtr, width, height );
    }
  }

  private boolean nativeRender( Bitmap input, String effect ) {
    logger.log( "nativeRender" );
    synchronized ( mNativeLock ) {
      return n_render( mPtr, input, effect );
    }
  }

  private boolean nativeWriteBitmap( Bitmap output ) {
    logger.log( "nativeWriteBitmap" );
    synchronized ( mNativeLock ) {
      return n_writeCurrentBitmap( mPtr, output );
    }
  }

  public <I, O> Future<O> submit( final Job<I, O> job, FutureListener<O> listener, final I... params ) {
    throw new RuntimeException( "Not Implemented" );
    
//    Worker<I, O> w = new Worker<I, O>( job, listener, params ) {
//
//      @Override
//      public void run() {
//        O result = null;
//
//        if ( setMode( ThreadMediaPool.MODE_CPU ) ) {
//          try {
//            result = job.run( this, params );
//          } catch ( Throwable ex ) {
//            Log.e( Worker.TAG, "Exception in running a job", ex );
//          }
//        }
//
//        synchronized ( this ) {
//          setMode( ThreadMediaPool.MODE_NONE );
//          setResult( result );
//          setIsDone();
//          notifyAll();
//        }
//
//        fireOnDoneEvent();
//      }
//    };
//    queueEvent( w );
//    return w;
  }

  private final Object mNativeLock = new Object();
  private final long mPtr;

  private static native long n_create();

  private static native boolean n_dispose( long ptr );

  private static native boolean n_initialize( long ptr );

  private static native boolean n_setRenderbufferSize( long ptr, int width, int heights );

  private static native boolean n_render( long ptr, Bitmap input, String effect );

  private static native boolean n_writeCurrentBitmap( long ptr, Bitmap output );

  class RenderJob implements Job<String, Boolean> {

    WeakReference<Bitmap> mBitmap;

    public RenderJob ( Bitmap bitmap ) {
      mBitmap = new WeakReference<Bitmap>( bitmap );
    }

    @Override
    public Boolean run( Worker<String, Boolean> worker, String... params ) throws Exception {
      logger.log( "RenderJob::run" );
      if ( null != mBitmap && null != mBitmap.get() ) {
        return nativeRender( mBitmap.get(), params[0] );
      }
      return false;
    }
  }

  class WriteBitmapJob implements Job<Void, Boolean> {

    WeakReference<Bitmap> mBitmap;

    public WriteBitmapJob ( Bitmap bitmap ) {
      mBitmap = new WeakReference<Bitmap>( bitmap );
    }

    @Override
    public Boolean run( Worker<Void, Boolean> worker, Void... params ) throws Exception {
      if ( null != mBitmap && null != mBitmap.get() ) {
        return nativeWriteBitmap( mBitmap.get() );
      }
      return false;
    }

  }

  class InitializeOpenGLJob implements Job<Void, Void> {
    
    @Override
    public Void run( Worker<Void, Void> worker, Void... params ) throws Exception {
      logger.log( "InitializeOpenGlJob::run" );
      nativeInitialize();
      logger.log( "end::nativeInitialize" );
      return null;
    }
  }

  class SetRenderbufferSizeJob implements Job<Integer, Void> {

    public SetRenderbufferSizeJob () {}

    @Override
    public Void run( Worker<Integer, Void> worker, Integer... params ) throws Exception {
      logger.log( "SetRenderbufferSizeJob::run" );
      nativeSetRenderbufferSize( params[0], params[1] );
      return null;
    }
  }

  private class AviaryGLRenderer implements GLSurfaceView.Renderer {

    private int mWidth, mHeight;

    @Override
    public void onDrawFrame( GL10 gl ) {
      logger.log( "onDrawFrame" );
    }

    @Override
    public void onSurfaceChanged( GL10 gl, int width, int height ) {
      logger.log( "onSurfaceChanged. " + width + "x" + height );

      final boolean changed = mWidth != width || mHeight != height;
      mWidth = width;
      mHeight = height;

      setRenderbufferSize( changed, width, height );
    }

    @Override
    public void onSurfaceCreated( GL10 gl, EGLConfig config ) {

      Log.d( "GL", "GL_RENDERER = " + gl.glGetString( GL10.GL_RENDERER ) );
      Log.d( "GL", "GL_VENDOR = " + gl.glGetString( GL10.GL_VENDOR ) );
      Log.d( "GL", "GL_VERSION = " + gl.glGetString( GL10.GL_VERSION ) );
      Log.i( "GL", "GL_EXTENSIONS = " + gl.glGetString( GL10.GL_EXTENSIONS ) );

      logger.log( "onSurfaceCreated" );
      initializeOpenGL();
    }
  }

  private GLRendererListener mGlRendererListener;

  public void setOnGlRendererListener( GLRendererListener listener ) {
    mGlRendererListener = listener;
  }

  private void fireOnSurfaceCreated() {
    if ( mGlRendererListener != null ) {
      mGlRendererListener.OnSurfaceCreated();
    }
  }

  private void fireOnSurfaceChanged( final boolean changed, final int width, final int height ) {
    if ( mGlRendererListener != null ) {
      getHandler().post( new Runnable() {

        @Override
        public void run() {
          mGlRendererListener.OnSurfaceChanged( changed, width, height );
        }
      } );
    }
  }

  public static interface GLRendererListener {

    public void OnSurfaceCreated();

    public void OnSurfaceChanged( boolean changed, int width, int height );
  }
}




Java Source Code List

com.aviary.android.feather.AlertActivity.java
com.aviary.android.feather.AviaryMainController.java
com.aviary.android.feather.FeatherActivity.java
com.aviary.android.feather.async_tasks.AsyncImageManager.java
com.aviary.android.feather.async_tasks.DownloadImageAsyncTask.java
com.aviary.android.feather.async_tasks.ExifTask.java
com.aviary.android.feather.effects.AbstractContentPanel.java
com.aviary.android.feather.effects.AbstractOptionPanel.java
com.aviary.android.feather.effects.AbstractPanelLoaderService.java
com.aviary.android.feather.effects.AbstractPanel.java
com.aviary.android.feather.effects.AdjustEffectPanel.java
com.aviary.android.feather.effects.BordersPanel.java
com.aviary.android.feather.effects.ColorSplashPanel.java
com.aviary.android.feather.effects.CropPanel.java
com.aviary.android.feather.effects.DelayedSpotDrawPanel.java
com.aviary.android.feather.effects.DrawingPanel.java
com.aviary.android.feather.effects.EffectsPanel.java
com.aviary.android.feather.effects.EnhanceEffectPanel.java
com.aviary.android.feather.effects.MemePanel.java
com.aviary.android.feather.effects.NativeEffectRangePanel.java
com.aviary.android.feather.effects.SimpleStatusMachine.java
com.aviary.android.feather.effects.SliderEffectPanel.java
com.aviary.android.feather.effects.StickersPanel.java
com.aviary.android.feather.effects.TextPanel.java
com.aviary.android.feather.effects.TiltShiftPanel.java
com.aviary.android.feather.graphics.CdsPreviewTransformer.java
com.aviary.android.feather.graphics.GalleryBottomIndicatorDrawable.java
com.aviary.android.feather.graphics.GalleryTopIndicatorDrawable.java
com.aviary.android.feather.graphics.GlowBitmapDrawable.java
com.aviary.android.feather.graphics.GlowDrawable.java
com.aviary.android.feather.graphics.PluginDividerDrawable.java
com.aviary.android.feather.graphics.PreviewFillColorDrawable.java
com.aviary.android.feather.graphics.PreviewSpotDrawable.java
com.aviary.android.feather.graphics.RepeatableHorizontalDrawable.java
com.aviary.android.feather.opengl.AviaryGLSurfaceView.java
com.aviary.android.feather.utils.PackIconCallable.java
com.aviary.android.feather.utils.SimpleBitmapCache.java
com.aviary.android.feather.utils.ThreadUtils.java
com.aviary.android.feather.utils.TypefaceUtils.java
com.aviary.android.feather.utils.UIUtils.java
com.aviary.android.feather.widget.AdjustImageView.java
com.aviary.android.feather.widget.AviaryAbsSpinner.java
com.aviary.android.feather.widget.AviaryAdapterView.java
com.aviary.android.feather.widget.AviaryBadgeToolLayout.java
com.aviary.android.feather.widget.AviaryBottomBarViewFlipper.java
com.aviary.android.feather.widget.AviaryButton.java
com.aviary.android.feather.widget.AviaryEdgeEffect.java
com.aviary.android.feather.widget.AviaryGalleryTopIndicatorView.java
com.aviary.android.feather.widget.AviaryGallery.java
com.aviary.android.feather.widget.AviaryHighlightImageButton.java
com.aviary.android.feather.widget.AviaryImageRestoreSwitcher.java
com.aviary.android.feather.widget.AviaryImageSwitcher.java
com.aviary.android.feather.widget.AviaryNavBarViewFlipper.java
com.aviary.android.feather.widget.AviarySeekBar.java
com.aviary.android.feather.widget.AviaryTextView.java
com.aviary.android.feather.widget.AviaryToast.java
com.aviary.android.feather.widget.AviaryToggleButton.java
com.aviary.android.feather.widget.AviaryWheel.java
com.aviary.android.feather.widget.AviaryWorkspaceIndicator.java
com.aviary.android.feather.widget.AviaryWorkspace.java
com.aviary.android.feather.widget.CellLayout.java
com.aviary.android.feather.widget.CropImageView.java
com.aviary.android.feather.widget.DrawableHighlightView.java
com.aviary.android.feather.widget.EffectThumbLayout.java
com.aviary.android.feather.widget.HighlightView.java
com.aviary.android.feather.widget.IAPBuyButton.java
com.aviary.android.feather.widget.IAPDialogDetail.java
com.aviary.android.feather.widget.IAPDialogList.java
com.aviary.android.feather.widget.IAPDialogMain.java
com.aviary.android.feather.widget.ImageViewDrawableOverlay.java
com.aviary.android.feather.widget.ImageViewSpotDraw.java
com.aviary.android.feather.widget.ImageViewTiltiShiftTouch.java
com.aviary.android.feather.widget.ImageViewTouchAndDraw.java
com.aviary.android.feather.widget.PointCloud.java
com.aviary.android.feather.widget.ScrollerRunnable.java
com.aviary.android.feather.widget.VibrationHelper.java
com.aviary.android.feather.widget.VibrationWidget.java