Equalizer  1.4.1
asyncFetcher.cpp
00001 
00002 /* Copyright (c) 2009-2011, Maxim Makhinya <maxmah@gmail.com>
00003  *                    2012, Stefan Eilemann <eile@eyescale.ch>
00004  *
00005  * Redistribution and use in source and binary forms, with or without
00006  * modification, are permitted provided that the following conditions are met:
00007  *
00008  * - Redistributions of source code must retain the above copyright notice, this
00009  *   list of conditions and the following disclaimer.
00010  * - Redistributions in binary form must reproduce the above copyright notice,
00011  *   this list of conditions and the following disclaimer in the documentation
00012  *   and/or other materials provided with the distribution.
00013  * - Neither the name of Eyescale Software GmbH nor the names of its
00014  *   contributors may be used to endorse or promote products derived from this
00015  *   software without specific prior written permission.
00016  *
00017  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
00018  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
00019  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
00020  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
00021  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
00022  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
00023  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
00024  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
00025  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
00026  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
00027  * POSSIBILITY OF SUCH DAMAGE.
00028  *
00029  */
00030 
00031 #include "asyncFetcher.h"
00032 #include "eqAsync.h"
00033 
00034 #include <eq/eq.h>
00035 #include <eq/client/system.h>
00036 
00037 namespace eqAsync
00038 {
00039 
00040 static eq::SystemWindow* initSharedContextWindow( eq::Window* window )
00041 {
00042     LBASSERT( window );
00043 
00044     // store old drawable of window and set window's drawable to OFF,
00045     // create another (shared) osWindow and restore original drawable
00046     const int32_t drawable =
00047         window->getIAttribute( eq::Window::IATTR_HINT_DRAWABLE );
00048     window->setIAttribute( eq::Window::IATTR_HINT_DRAWABLE, eq::OFF );
00049 
00050     const eq::Pipe* pipe = window->getPipe();
00051     LBASSERT( pipe );
00052 
00053     eq::SystemWindow* sharedWindow =
00054         pipe->getWindowSystem().createWindow( window );
00055 
00056     if( sharedWindow )
00057     {
00058         if( sharedWindow->configInit( ))
00059             sharedWindow->makeCurrent();
00060         else
00061         {
00062             LBWARN << "OS Window initialization failed: " << std::endl;
00063             delete sharedWindow;
00064             sharedWindow = 0;
00065         }
00066     }
00067     else
00068     {
00069         LBERROR << "Failed to create shared context window for "
00070                 << pipe->getWindowSystem() << std::endl;
00071     }
00072 
00073     window->setIAttribute( eq::Window::IATTR_HINT_DRAWABLE, drawable );
00074 
00075     LBINFO << "Async fetcher initialization finished" << std::endl;
00076     return sharedWindow;
00077 }
00078 
00079 
00080 static void deleteSharedContextWindow( eq::Window* window,
00081                                        eq::SystemWindow** sharedWindow,
00082                                        eq::ObjectManager** objectManager )
00083 {
00084     LBWARN << "Deleting shared context" << std::endl;
00085     if( !sharedWindow || !*sharedWindow )
00086         return;
00087 
00088     if( *objectManager )
00089     {
00090         (*objectManager)->deleteAll();
00091         delete *objectManager;
00092         *objectManager = 0;
00093     }
00094 
00095     (*sharedWindow)->configExit(); // mb set window to 0 before that?
00096 
00097     delete *sharedWindow;
00098     *sharedWindow = 0;
00099 }
00100 
00101 
00102 AsyncFetcher::AsyncFetcher()
00103     : lunchbox::Thread()
00104     , _window( 0 )
00105     , _objectManager( 0 )
00106     , _sharedWindow( 0 )
00107 {
00108     _tmpTexture = new GLbyte[ 64*64*4 ];
00109 }
00110 
00111 
00112 AsyncFetcher::~AsyncFetcher()
00113 {
00114     if( _window && _sharedWindow )
00115         deleteSharedContextWindow( _window, &_sharedWindow, &_objectManager );
00116 
00117     delete [] _tmpTexture;
00118 }
00119 
00120 
00121 const GLEWContext* AsyncFetcher::glewGetContext() const
00122 {
00123     return _sharedWindow->glewGetContext();
00124 }
00125 
00126 
00131 void AsyncFetcher::run()
00132 {
00133     LBASSERT( !_sharedWindow );
00134     _sharedWindow = initSharedContextWindow( _window );
00135     _outQueue.push( TextureId( )); // unlock pipe thread
00136     if( !_sharedWindow )
00137         return;
00138 
00139     _objectManager = new eq::ObjectManager( glewGetContext( ));
00140     LBINFO << "async fetcher initialized: " << _window << std::endl;
00141 
00142     uint8_t* i = 0;
00143     bool running = true;
00144     lunchbox::sleep( 1000 ); // imitate loading of the first texture
00145     while( running )
00146     {
00147         // generate new texture
00148         eq::util::Texture* tx = _objectManager->newEqTexture( ++i,
00149                                                               GL_TEXTURE_2D );
00150         tx->init( GL_RGBA8, 64, 64 );
00151 
00152         int j = 0;
00153         lunchbox::RNG rng;
00154         for( int y = 0; y < 64; ++y )
00155         {
00156             for( int x = 0; x < 64; ++x )
00157             {
00158                 const GLbyte rnd = rng.get< uint8_t >() % 127;
00159                 const GLbyte val = (x / 8) % 2 == (y / 8) % 2 ? rnd : 0;
00160                 _tmpTexture[ j++ ] = val;
00161                 _tmpTexture[ j++ ] = val;
00162                 _tmpTexture[ j++ ] = val;
00163                 _tmpTexture[ j++ ] = val;
00164             }
00165         }
00166         tx->upload( 64, 64, _tmpTexture );
00167         EQ_GL_CALL( glFinish( ));
00168 
00169         // add new texture to the pool
00170         _outQueue.push( TextureId( tx->getName(), i ));
00171 
00172         // imitate hard work of loading something else
00173         lunchbox::sleep( rng.get< uint32_t >() % 5000u );
00174 
00175         // clean unused textures
00176         const void* keyToDelete = 0;
00177         while( _inQueue.tryPop( keyToDelete ))
00178         {
00179             if( keyToDelete )
00180             {
00181                 LBWARN << "Deleting eq texture " << keyToDelete << std::endl;
00182                 _objectManager->deleteEqTexture( keyToDelete );
00183             }
00184             else
00185                 running = false;
00186         }
00187     }
00188     deleteSharedContextWindow( _window, &_sharedWindow, &_objectManager );
00189 }
00190 
00191 } //namespace eqAsync
Generated on Mon Nov 26 2012 14:41:48 for Equalizer 1.4.1 by  doxygen 1.7.6.1