Activity
---------------------------------------------------------------
package com.bsidesoft.test;
import com.bsidesoft.adr.bs;
import android.app.Activity;
import android.os.Bundle;
public class bsTest0 extends Activity{
private bs _bs;
@Override
public void onCreate( Bundle $savedInstanceState ){
super.onCreate( $savedInstanceState );
_bs = new bs( this, $savedInstanceState, "bs", "file:///android_asset/url.html" );
setContentView( _bs );
}
@Override
protected void onStart(){
super.onStart();
_bs.onStart();
}
@Override
protected void onResume(){
super.onResume();
_bs.onResume();
}
@Override
protected void onPause(){
super.onPause();
_bs.onPause();
}
@Override
protected void onStop(){
super.onStop();
_bs.onStop();
}
@Override
protected void onDestroy(){
super.onDestroy();
_bs.onDestroy();
}
}
--------------------------------------------------------------------
SurfaceView
final public class bs extends SurfaceView implements Callback, Runnable{
final static int MAX_LAYER = 5;
private Thread _thread;
private boolean _isRunning = false;
private boolean _isSurfaceChanged = false;
private Activity _act;
private SurfaceHolder _holder;
private GL10 _gl;
private EGL10 _egl;
private EGLDisplay _eglDisplay;
private EGLConfig _eglConfig;
private EGLContext _eglContext;
private EGLSurface _eglSurface;
private int _width;
private int _height;
private boolean _isLandscape;
private bsLayer[] _layers = {null, null, null, null, null};
private bsLayer _curr;
private TextureManager _textureManager;
public bs( Activity $act, Bundle $bundle, String $storagePath, String $url ){
super( $act.getApplication() );
_act = $act;
_holder = getHolder();
_holder.addCallback( this );
setFocusable( true );
requestFocus();
setFocusableInTouchMode( true );
_textureManager = TextureManager.getInstance();
}
public void onStart(){
Slog("onStart");
}
public void onResume(){
Slog("onResume");
_isRunning = true;
_thread = new Thread( this );
_thread.start();
}
@Override
public void run(){
while( _isRunning ){
if( _isSurfaceChanged ){
for( int i = 0 ; i < MAX_LAYER ; i++ ) if( _layers != null ) _layers.run();
}
}
}
public void onPause(){
Slog("onPause");
_isSurfaceChanged = _isRunning = false;
while( true ){
try{
_thread.join(); break;
}catch( Exception $e ){}
}
}
public void onStop(){
Slog("onStop");
}
public void onDestroy(){
Slog("onDestroy");
}
void Slog( String $log ){
Log.i( "bs", $log );
}
void start(){
Ladd( 0 );
_textureManager.addTexture( "a", new Texture( 10, 10, new RGBColor( 255, 0, 0 ) ) );
Object3D plane = Primitives.getPlane( 1, 1 );
plane.setTexture( "a" );
_layers[0].world.addObject( plane );
}
@Override
public void surfaceCreated( SurfaceHolder holder ){}
@Override
public void surfaceChanged( SurfaceHolder holder, int format, int width, int height ){
Slog("surfaceChanged");
boolean isStart = false;
if( _gl == null ){
if( !init() ){
Slog( "fail" );
return;
}
isStart = true;
}else{
restart();
}
_isLandscape = _act.getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE;
for( int i = 0 ; i < MAX_LAYER ; i++ ) if( _layers != null ) Ladd( i, false );
_isSurfaceChanged = true;
if( isStart ) start();
}
public void Ladd( int $index){
Ladd( $index, true );
}
void Ladd( int $index, boolean $isInit ){
synchronized(this){
bsLayer layer = new bsLayer();
FrameBuffer fb = layer.fb;
if( fb != null ) fb.dispose();
layer.fb = new FrameBuffer( _gl, _width, _height );
if( $isInit ){
layer.world = new World();
layer.world.setAmbientLight( 255, 255, 255 );
layer.camera = layer.world.getCamera();
}
_layers[$index] = layer;
}
}
private boolean init(){
_egl = (EGL10) EGLContext.getEGL();
_eglDisplay = _egl.eglGetDisplay( EGL10.EGL_DEFAULT_DISPLAY );
{
int[] version = {-1, -1};
if( !_egl.eglInitialize( _eglDisplay, version ) ){
Slog( "eglInit fail " );
return false;
}
}
{
EGLConfig[] configs = new EGLConfig[1];
int[] num = new int[1];
int[] spec = {EGL10.EGL_DEPTH_SIZE, 16, EGL10.EGL_NONE};
if( ! _egl.eglChooseConfig( _eglDisplay, spec, configs, 1, num) ){
Slog( "eglChooseConfig fail " );
return false;
}
_eglConfig = configs[0];
}
{
_eglContext = _egl.eglCreateContext( _eglDisplay, _eglConfig, EGL10.EGL_NO_CONTEXT, null );
if( _eglContext == EGL10.EGL_NO_CONTEXT ){
Slog( "eglCreateContext fail " );
return false;
}
}
{
_eglSurface = _egl.eglCreateWindowSurface( _eglDisplay, _eglConfig, _holder, null );
if( _eglSurface == EGL10.EGL_NO_SURFACE ){
Slog( "eglCreateWindowSurface fail " );
return false;
}
}
_gl = (GL10) _eglContext.getGL();
{
if( ! _egl.eglMakeCurrent( _eglDisplay, _eglSurface, _eglSurface, _eglContext ) ){
Slog( "eglMakeCurrent fail " );
return false;
}
}
{
Rect rect = _holder.getSurfaceFrame();
_width = rect.width();
_height = rect.height();
}
_egl.eglSwapBuffers(_eglDisplay, _eglSurface);
return true;
}
private void restart(){
}
@Override
public void surfaceDestroyed( SurfaceHolder holder ){
}
}
---------------------------------------------------------------
Layer
final class bsLayer{
static private RGBColor back = new RGBColor( 0, 0, 0, 0 );
FrameBuffer fb;
World world;
Camera camera;
void run(){
fb.clear( back );
world.renderScene( fb );
world.draw( fb );
fb.display();
}
}
----------------------------------------------------------------
flow
1. onStart - onResume - surfaceChanged
2. start ( after onDestroy )
3. thread - run()
4. world.draw()
5. GLRenderer.convertTexture()
----------------------------------------------------------------
please, what's the problem in custom SurfaceView?
----------------------------------------------------------------
error stack
bs [Android Application]
DalvikVM[localhost:8601]
Thread [<1> main] (Running)
Thread [<10> Binder Thread #2] (Running)
Thread [<9> Binder Thread #1] (Running)
Daemon Thread [<8> FinalizerWatchdogDaemon] (Running)
Daemon Thread [<7> FinalizerDaemon] (Running)
Daemon Thread [<6> ReferenceQueueDaemon] (Running)
Thread [<11> Thread-3942] (Suspended (exception RuntimeException))
Logger.log(String, int) line: 189
GLRenderer.convertTexture(Texture) line: 982
GLRenderer.setTextures(Object3D, int, int, FrameBuffer, World) line: 2339
GLRenderer.drawVertexArray(VisList, int, int, FrameBuffer, World) line: 2252
World.draw(FrameBuffer, boolean, int) line: 1354
World.draw(FrameBuffer) line: 1135
bsLayer.run() line: 19
bs.run() line: 81
Thread.run() line: 856
Thread [<12> Binder Thread #3] (Running)