Equalizer  1.8.0
Parallel Rendering Framework
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Pages
asyncFetcher.cpp
1 
2 /* Copyright (c) 2009-2011, Maxim Makhinya <maxmah@gmail.com>
3  * 2012-2013, Stefan Eilemann <eile@eyescale.ch>
4  * 2014, Daniel Nachbaur <danielnachbaur@gmail.com>
5  *
6  * Redistribution and use in source and binary forms, with or without
7  * modification, are permitted provided that the following conditions are met:
8  *
9  * - Redistributions of source code must retain the above copyright notice, this
10  * list of conditions and the following disclaimer.
11  * - Redistributions in binary form must reproduce the above copyright notice,
12  * this list of conditions and the following disclaimer in the documentation
13  * and/or other materials provided with the distribution.
14  * - Neither the name of Eyescale Software GmbH nor the names of its
15  * contributors may be used to endorse or promote products derived from this
16  * software without specific prior written permission.
17  *
18  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
19  * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
20  * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
21  * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
22  * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
23  * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
24  * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
25  * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
26  * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
27  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
28  * POSSIBILITY OF SUCH DAMAGE.
29  *
30  */
31 
32 #include "asyncFetcher.h"
33 #include "eqAsync.h"
34 
35 #include <eq/eq.h>
36 #include <eq/client/system.h>
37 
38 namespace eqAsync
39 {
40 AsyncFetcher::AsyncFetcher()
41  : lunchbox::Thread()
42  , _sharedWindow( 0 )
43 {
44 }
45 
46 AsyncFetcher::~AsyncFetcher()
47 {
48  stop();
49 }
50 
51 const GLEWContext* AsyncFetcher::glewGetContext() const
52 {
53  return _sharedWindow->glewGetContext();
54 }
55 
56 static eq::SystemWindow* initSharedContextWindow( eq::Window* window )
57 {
58  LBASSERT( window );
59 
60  eq::WindowSettings settings = window->getSettings();
62  const eq::Pipe* pipe = window->getPipe();
63  eq::SystemWindow* sharedWindow =
64  pipe->getWindowSystem().createWindow( window, settings );
65 
66  if( sharedWindow )
67  {
68  if( sharedWindow->configInit( ))
69  sharedWindow->makeCurrent();
70  else
71  {
72  LBWARN << "OS Window initialization failed: " << std::endl;
73  delete sharedWindow;
74  sharedWindow = 0;
75  }
76  }
77  else
78  {
79  LBERROR << "Failed to create shared context window for "
80  << pipe->getWindowSystem() << std::endl;
81  }
82 
83  window->makeCurrent();
84 
85  LBINFO << "Async fetcher initialization finished" << std::endl;
86  return sharedWindow;
87 }
88 
89 void AsyncFetcher::setup( Window* window )
90 {
91  _sharedWindow = initSharedContextWindow( window );
92  start();
93 }
94 
95 void AsyncFetcher::stop()
96 {
97  if( !_sharedWindow )
98  return;
99 
100  deleteTexture( 0 ); // exit async thread
101  join();
102 
103  _sharedWindow->configExit();
104  delete _sharedWindow;
105  _sharedWindow = 0;
106 }
107 
112 void AsyncFetcher::run()
113 {
114  LBASSERT( _sharedWindow );
115  if( !_sharedWindow )
116  return;
117 
118  _sharedWindow->makeCurrent();
119  eq::util::ObjectManager objects( glewGetContext( ));
120  lunchbox::Bufferb textureData( 64*64*4 );
121  LBINFO << "async fetcher initialized" << std::endl;
122 
123  bool running = true;
124  lunchbox::sleep( 1000 ); // imitate loading of the first texture
125  for( uint8_t* i = 0; running; ++i )
126  {
127  // generate new texture
128  eq::util::Texture* tx = objects.newEqTexture( i, GL_TEXTURE_2D );
129  tx->init( GL_RGBA8, 64, 64 );
130 
131  int j = 0;
132  lunchbox::RNG rng;
133  for( int y = 0; y < 64; ++y )
134  {
135  for( int x = 0; x < 64; ++x )
136  {
137  const GLbyte rnd = rng.get< uint8_t >() % 127;
138  const GLbyte val = (x / 8) % 2 == (y / 8) % 2 ? rnd : 0;
139  textureData[ j++ ] = val;
140  textureData[ j++ ] = val;
141  textureData[ j++ ] = val;
142  textureData[ j++ ] = val;
143  }
144  }
145  tx->upload( 64, 64, textureData.getData( ));
146  EQ_GL_CALL( glFinish( ));
147 
148  // add new texture to the pool
149  _outQueue.push( TextureId( tx->getName(), i ));
150 
151  // imitate hard work of loading something else
152  lunchbox::sleep( rng.get< uint32_t >() % 5000u );
153 
154  // clean unused textures
155  const void* keyToDelete = 0;
156  while( _inQueue.tryPop( keyToDelete ))
157  {
158  if( keyToDelete )
159  {
160  LBWARN << "Deleting eq texture " << keyToDelete << std::endl;
161  objects.deleteEqTexture( keyToDelete );
162  }
163  else
164  running = false;
165  }
166  }
167  objects.deleteAll();
168 }
169 
170 } //namespace eqAsync
A Pipe represents a graphics card (GPU) on a Node.
A set of settings to setup an eq::SystemWindow.
A wrapper around OpenGL textures.
Definition: texture.h:38
EQFABRIC_API bool setIAttribute(const IAttribute attr, const int32_t value)
Set a window attribute.
EQ_API void upload(const int32_t width, const int32_t height, const void *ptr)
Copy the specified buffer to the texture at 0,0.
EQ_API void init(const unsigned internalFormat, const int32_t width, const int32_t height)
Initialize an OpenGL texture.
A Window represents an on-screen or off-screen drawable.
EQFABRIC_INL const Settings & getSettings() const
virtual EQ_API bool configInit()=0
Initialize this system window.
virtual EQ_API void makeCurrent(const bool cache=true) const
Make the window's drawable and context current.
Structure to associate OpenGL texture ids with an external key.
Definition: asyncFetcher.h:42
The interface definition for system-specific windowing code.
Definition: systemWindow.h:35
const P * getPipe() const
virtual EQ_API void makeCurrent(const bool cache=true) const =0
Make the system window rendering context and drawable current.
A facility class to manage OpenGL objects across shared contexts.
Definition: objectManager.h:51
EQ_API WindowSystem getWindowSystem() const
Return the window system used by this pipe.
EQ_API unsigned getName() const
EQ_API void deleteAll()
Delete all managed objects and associated GL objects.