[3.0] Stop calling destroy methods of texture and FrameBuffers on RenderManager destr...
[platform/core/uifw/dali-core.git] / dali / internal / render / common / render-manager.cpp
index c6aa9b9..269cec0 100644 (file)
@@ -1,5 +1,5 @@
 /*
- * Copyright (c) 2014 Samsung Electronics Co., Ltd.
+ * Copyright (c) 2016 Samsung Electronics Co., Ltd.
  *
  * Licensed under the Apache License, Version 2.0 (the "License");
  * you may not use this file except in compliance with the License.
@@ -26,7 +26,6 @@
 #include <dali/integration-api/debug.h>
 #include <dali/integration-api/core.h>
 #include <dali/internal/common/owner-pointer.h>
-#include <dali/internal/render/queue/render-queue.h>
 #include <dali/internal/render/common/render-algorithms.h>
 #include <dali/internal/render/common/render-debug.h>
 #include <dali/internal/render/common/render-tracker.h>
 #include <dali/internal/render/gl-resources/context.h>
 #include <dali/internal/render/gl-resources/frame-buffer-texture.h>
 #include <dali/internal/render/gl-resources/texture-cache.h>
-#include <dali/internal/render/renderers/render-renderer.h>
+#include <dali/internal/render/queue/render-queue.h>
+#include <dali/internal/render/renderers/render-frame-buffer.h>
 #include <dali/internal/render/renderers/render-geometry.h>
+#include <dali/internal/render/renderers/render-renderer.h>
 #include <dali/internal/render/renderers/render-sampler.h>
 #include <dali/internal/render/shaders/program-controller.h>
 
-// Uncomment the next line to enable frame snapshot logging
-//#define FRAME_SNAPSHOT_LOGGING
-
-#ifdef FRAME_SNAPSHOT_LOGGING
-
-
-namespace // unnamed namespace
-{
-unsigned int SNAPSHOT_FRAME_FREQUENCY = 1200; // dump every 20-30 seconds
-} // unnamed namespace
-
-#define SET_SNAPSHOT_FRAME_LOG_LEVEL \
-  DALI_LOG_FILTER_SET_LEVEL(Context::gGlLogFilter, ((GetFrameCount() % SNAPSHOT_FRAME_FREQUENCY)==5 ? Debug::General : Debug::Concise));
-
-#else // FRAME_SNAPSHOT_LOGGING
-
-#define SET_SNAPSHOT_FRAME_LOG_LEVEL
-
-#endif // FRAME_SNAPSHOT_LOGGING
-
 namespace Dali
 {
 
@@ -72,31 +53,39 @@ namespace SceneGraph
 typedef OwnerContainer< Render::Renderer* >    RendererOwnerContainer;
 typedef RendererOwnerContainer::Iterator       RendererOwnerIter;
 
-typedef OwnerContainer< RenderGeometry* >      RenderGeometryOwnerContainer;
-typedef RenderGeometryOwnerContainer::Iterator RenderGeometryOwnerIter;
+typedef OwnerContainer< Render::Geometry* >    GeometryOwnerContainer;
+typedef GeometryOwnerContainer::Iterator       GeometryOwnerIter;
 
 typedef OwnerContainer< Render::Sampler* >    SamplerOwnerContainer;
 typedef SamplerOwnerContainer::Iterator       SamplerOwnerIter;
 
+typedef OwnerContainer< Render::NewTexture* >   TextureOwnerContainer;
+typedef TextureOwnerContainer::Iterator         TextureOwnerIter;
+
+typedef OwnerContainer< Render::FrameBuffer* >  FrameBufferOwnerContainer;
+typedef FrameBufferOwnerContainer::Iterator     FrameBufferOwnerIter;
+
 typedef OwnerContainer< Render::PropertyBuffer* > PropertyBufferOwnerContainer;
 typedef PropertyBufferOwnerContainer::Iterator    PropertyBufferOwnerIter;
 
-typedef OwnerContainer< RenderTracker* >       RenderTrackerContainer;
-typedef RenderTrackerContainer::Iterator       RenderTrackerIter;
-typedef RenderTrackerContainer::ConstIterator  RenderTrackerConstIter;
+typedef OwnerContainer< Render::RenderTracker* > RenderTrackerContainer;
+typedef RenderTrackerContainer::Iterator         RenderTrackerIter;
+typedef RenderTrackerContainer::ConstIterator    RenderTrackerConstIter;
 
 /**
  * Structure to contain internal data
  */
 struct RenderManager::Impl
 {
-  Impl( Dali::Integration::GlAbstraction& glAbstraction,
-        ResourcePostProcessList& resourcePostProcessQ,
-        PostProcessResourceDispatcher& postProcessDispatcher )
+  Impl( Integration::GlAbstraction& glAbstraction,
+        Integration::GlSyncAbstraction& glSyncAbstraction,
+        LockedResourceQueue& textureUploadedQ,
+        TextureUploadedDispatcher& postProcessDispatcher )
   : context( glAbstraction ),
+    glSyncAbstraction( glSyncAbstraction ),
     renderQueue(),
     textureCache( renderQueue, postProcessDispatcher, context ),
-    resourcePostProcessQueue( resourcePostProcessQ ),
+    textureUploadedQueue( textureUploadedQ ),
     instructions(),
     backgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ),
     frameCount( 0 ),
@@ -104,6 +93,8 @@ struct RenderManager::Impl
     defaultSurfaceRect(),
     rendererContainer(),
     samplerContainer(),
+    textureContainer(),
+    frameBufferContainer(),
     renderersAdded( false ),
     firstRenderCompleted( false ),
     defaultShader( NULL ),
@@ -115,13 +106,13 @@ struct RenderManager::Impl
   {
   }
 
-  void AddRenderTracker( RenderTracker* renderTracker )
+  void AddRenderTracker( Render::RenderTracker* renderTracker )
   {
     DALI_ASSERT_DEBUG( renderTracker != NULL );
     mRenderTrackers.PushBack( renderTracker );
   }
 
-  void RemoveRenderTracker( RenderTracker* renderTracker )
+  void RemoveRenderTracker( Render::RenderTracker* renderTracker )
   {
     DALI_ASSERT_DEBUG( renderTracker != NULL );
     for(RenderTrackerIter iter = mRenderTrackers.Begin(), end = mRenderTrackers.End(); iter != end; ++iter)
@@ -145,9 +136,10 @@ struct RenderManager::Impl
   // the order is important for destruction,
   // programs are owned by context at the moment.
   Context                       context;                  ///< holds the GL state
+  Integration::GlSyncAbstraction& glSyncAbstraction;      ///< GL sync abstraction
   RenderQueue                   renderQueue;              ///< A message queue for receiving messages from the update-thread.
   TextureCache                  textureCache;             ///< Cache for all GL textures
-  ResourcePostProcessList&      resourcePostProcessQueue; ///< A queue for requesting resource post processing in update thread
+  LockedResourceQueue&          textureUploadedQueue;     ///< A queue for requesting resource post processing in update thread
 
   // Render instructions describe what should be rendered during RenderManager::Render()
   // Owned by RenderManager. Update manager updates instructions for the next frame while we render the current one
@@ -162,8 +154,10 @@ struct RenderManager::Impl
 
   RendererOwnerContainer        rendererContainer;        ///< List of owned renderers
   SamplerOwnerContainer         samplerContainer;         ///< List of owned samplers
+  TextureOwnerContainer         textureContainer;         ///< List of owned textures
+  FrameBufferOwnerContainer     frameBufferContainer;     ///< List of owned framebuffers
   PropertyBufferOwnerContainer  propertyBufferContainer;  ///< List of owned property buffers
-  RenderGeometryOwnerContainer  renderGeometryContainer;  ///< List of owned RenderGeometries
+  GeometryOwnerContainer        geometryContainer;        ///< List of owned Geometries
 
   bool                          renderersAdded;
 
@@ -174,10 +168,12 @@ struct RenderManager::Impl
   ProgramController             programController;        ///< Owner of the GL programs
 };
 
-RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction, ResourcePostProcessList& resourcePostProcessQ )
+RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction,
+                                   Integration::GlSyncAbstraction& glSyncAbstraction,
+                                   LockedResourceQueue& textureUploadedQ )
 {
   RenderManager* manager = new RenderManager;
-  manager->mImpl = new Impl( glAbstraction, resourcePostProcessQ, *manager );
+  manager->mImpl = new Impl( glAbstraction, glSyncAbstraction, textureUploadedQ, *manager );
   return manager;
 }
 
@@ -212,15 +208,24 @@ void RenderManager::ContextCreated()
 
 void RenderManager::ContextDestroyed()
 {
-  // @todo Set an atomic value to prevent render manager rendering again until
-  // ContextCreated has been called.
-
   mImpl->context.GlContextDestroyed();
   mImpl->programController.GlContextDestroyed();
 
   // inform texture cache
   mImpl->textureCache.GlContextDestroyed(); // Clears gl texture ids
 
+  //Inform textures
+  for( TextureOwnerIter iter = mImpl->textureContainer.Begin(); iter != mImpl->textureContainer.End(); ++iter )
+  {
+    (*iter)->GlContextDestroyed();
+  }
+
+  //Inform framebuffers
+  for( FrameBufferOwnerIter iter = mImpl->frameBufferContainer.Begin(); iter != mImpl->frameBufferContainer.End(); ++iter )
+  {
+    (*iter)->GlContextDestroyed();
+  }
+
   // inform renderers
   RendererOwnerContainer::Iterator end = mImpl->rendererContainer.End();
   RendererOwnerContainer::Iterator iter = mImpl->rendererContainer.Begin();
@@ -231,9 +236,9 @@ void RenderManager::ContextDestroyed()
   }
 }
 
-void RenderManager::DispatchPostProcessRequest(ResourcePostProcessRequest& request)
+void RenderManager::DispatchTextureUploaded(ResourceId request)
 {
-  mImpl->resourcePostProcessQueue[ mImpl->renderBufferIndex ].push_back( request );
+  mImpl->textureUploadedQueue.PushBack( request );
 }
 
 void RenderManager::SetShaderSaver( ShaderSaver& upstream )
@@ -308,14 +313,80 @@ void RenderManager::RemoveSampler( Render::Sampler* sampler )
   }
 }
 
+void RenderManager::AddTexture( Render::NewTexture* texture )
+{
+  mImpl->textureContainer.PushBack( texture );
+  texture->Initialize(mImpl->context);
+}
+
+void RenderManager::RemoveTexture( Render::NewTexture* texture )
+{
+  DALI_ASSERT_DEBUG( NULL != texture );
+
+  TextureOwnerContainer& textures = mImpl->textureContainer;
+
+  // Find the texture
+  for ( TextureOwnerIter iter = textures.Begin(); iter != textures.End(); ++iter )
+  {
+    if ( *iter == texture )
+    {
+      texture->Destroy( mImpl->context );
+      textures.Erase( iter ); // Texture found; now destroy it
+      break;
+    }
+  }
+}
+
+void RenderManager::UploadTexture( Render::NewTexture* texture, PixelDataPtr pixelData, const NewTexture::UploadParams& params )
+{
+  texture->Upload( mImpl->context, pixelData, params );
+}
+
+void RenderManager::GenerateMipmaps( Render::NewTexture* texture )
+{
+  texture->GenerateMipmaps( mImpl->context );
+}
+
 void RenderManager::SetFilterMode( Render::Sampler* sampler, unsigned int minFilterMode, unsigned int magFilterMode )
 {
-  sampler->SetFilterMode( (Dali::FilterMode::Type)minFilterMode, (Dali::FilterMode::Type)magFilterMode );
+  sampler->mMinificationFilter = static_cast<Dali::FilterMode::Type>(minFilterMode);
+  sampler->mMagnificationFilter = static_cast<Dali::FilterMode::Type>(magFilterMode );
+}
+
+void RenderManager::SetWrapMode( Render::Sampler* sampler, unsigned int rWrapMode, unsigned int sWrapMode, unsigned int tWrapMode )
+{
+  sampler->mRWrapMode = static_cast<Dali::WrapMode::Type>(rWrapMode);
+  sampler->mSWrapMode = static_cast<Dali::WrapMode::Type>(sWrapMode);
+  sampler->mTWrapMode = static_cast<Dali::WrapMode::Type>(tWrapMode);
+}
+
+void RenderManager::AddFrameBuffer( Render::FrameBuffer* frameBuffer )
+{
+  mImpl->frameBufferContainer.PushBack( frameBuffer );
+  frameBuffer->Initialize(mImpl->context);
+}
+
+void RenderManager::RemoveFrameBuffer( Render::FrameBuffer* frameBuffer )
+{
+  DALI_ASSERT_DEBUG( NULL != frameBuffer );
+
+  FrameBufferOwnerContainer& framebuffers = mImpl->frameBufferContainer;
+
+  // Find the sampler
+  for ( FrameBufferOwnerIter iter = framebuffers.Begin(); iter != framebuffers.End(); ++iter )
+  {
+    if ( *iter == frameBuffer )
+    {
+      frameBuffer->Destroy( mImpl->context );
+      framebuffers.Erase( iter ); // frameBuffer found; now destroy it
+      break;
+    }
+  }
 }
 
-void RenderManager::SetWrapMode( Render::Sampler* sampler, unsigned int uWrapMode, unsigned int vWrapMode )
+void RenderManager::AttachColorTextureToFrameBuffer( Render::FrameBuffer* frameBuffer, Render::NewTexture* texture, unsigned int mipmapLevel, unsigned int layer )
 {
-  sampler->SetWrapMode( (Dali::WrapMode::Type)uWrapMode, (Dali::WrapMode::Type)vWrapMode );
+  frameBuffer->AttachColorTexture( mImpl->context, texture, mipmapLevel, layer );
 }
 
 void RenderManager::AddPropertyBuffer( Render::PropertyBuffer* propertyBuffer )
@@ -345,31 +416,31 @@ void RenderManager::SetPropertyBufferFormat(Render::PropertyBuffer* propertyBuff
   propertyBuffer->SetFormat( format );
 }
 
-void RenderManager::SetPropertyBufferData(Render::PropertyBuffer* propertyBuffer, Dali::Vector<char>* data)
+void RenderManager::SetPropertyBufferData( Render::PropertyBuffer* propertyBuffer, Dali::Vector<char>* data, size_t size )
 {
-  propertyBuffer->SetData( data );
+  propertyBuffer->SetData( data, size );
 }
 
-void RenderManager::SetPropertyBufferSize(Render::PropertyBuffer* propertyBuffer, size_t size )
+void RenderManager::SetIndexBuffer( Render::Geometry* geometry, Dali::Vector<unsigned short>& indices )
 {
-  propertyBuffer->SetSize( size );
+  geometry->SetIndexBuffer( indices );
 }
 
-void RenderManager::AddGeometry( RenderGeometry* renderGeometry )
+void RenderManager::AddGeometry( Render::Geometry* geometry )
 {
-  mImpl->renderGeometryContainer.PushBack( renderGeometry );
+  mImpl->geometryContainer.PushBack( geometry );
 }
 
-void RenderManager::RemoveGeometry( RenderGeometry* renderGeometry )
+void RenderManager::RemoveGeometry( Render::Geometry* geometry )
 {
-  DALI_ASSERT_DEBUG( NULL != renderGeometry );
+  DALI_ASSERT_DEBUG( NULL != geometry );
 
-  RenderGeometryOwnerContainer& geometries = mImpl->renderGeometryContainer;
+  GeometryOwnerContainer& geometries = mImpl->geometryContainer;
 
   // Find the geometry
-  for ( RenderGeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
+  for ( GeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
   {
-    if ( *iter == renderGeometry )
+    if ( *iter == geometry )
     {
       geometries.Erase( iter ); // Geometry found; now destroy it
       break;
@@ -377,33 +448,33 @@ void RenderManager::RemoveGeometry( RenderGeometry* renderGeometry )
   }
 }
 
-void RenderManager::AddPropertyBuffer( RenderGeometry* renderGeometry, Render::PropertyBuffer* propertyBuffer, bool isIndexBuffer )
+void RenderManager::AddVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer )
 {
-  DALI_ASSERT_DEBUG( NULL != renderGeometry );
+  DALI_ASSERT_DEBUG( NULL != geometry );
 
-  RenderGeometryOwnerContainer& geometries = mImpl->renderGeometryContainer;
+  GeometryOwnerContainer& geometries = mImpl->geometryContainer;
 
   // Find the renderer
-  for ( RenderGeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
+  for ( GeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
   {
-    if ( *iter == renderGeometry )
+    if ( *iter == geometry )
     {
-      (*iter)->AddPropertyBuffer( propertyBuffer, isIndexBuffer );
+      (*iter)->AddPropertyBuffer( propertyBuffer );
       break;
     }
   }
 }
 
-void RenderManager::RemovePropertyBuffer( RenderGeometry* renderGeometry, Render::PropertyBuffer* propertyBuffer )
+void RenderManager::RemoveVertexBuffer( Render::Geometry* geometry, Render::PropertyBuffer* propertyBuffer )
 {
-  DALI_ASSERT_DEBUG( NULL != renderGeometry );
+  DALI_ASSERT_DEBUG( NULL != geometry );
 
-  RenderGeometryOwnerContainer& geometries = mImpl->renderGeometryContainer;
+  GeometryOwnerContainer& geometries = mImpl->geometryContainer;
 
   // Find the renderer
-  for ( RenderGeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
+  for ( GeometryOwnerIter iter = geometries.Begin(); iter != geometries.End(); ++iter )
   {
-    if ( *iter == renderGeometry )
+    if ( *iter == geometry )
     {
       (*iter)->RemovePropertyBuffer( propertyBuffer );
       break;
@@ -411,13 +482,17 @@ void RenderManager::RemovePropertyBuffer( RenderGeometry* renderGeometry, Render
   }
 }
 
+void RenderManager::SetGeometryType( Render::Geometry* geometry, unsigned int geometryType )
+{
+  geometry->SetType( Render::Geometry::Type(geometryType) );
+}
 
-void RenderManager::AddRenderTracker( RenderTracker* renderTracker )
+void RenderManager::AddRenderTracker( Render::RenderTracker* renderTracker )
 {
   mImpl->AddRenderTracker(renderTracker);
 }
 
-void RenderManager::RemoveRenderTracker( RenderTracker* renderTracker )
+void RenderManager::RemoveRenderTracker( Render::RenderTracker* renderTracker )
 {
   mImpl->RemoveRenderTracker(renderTracker);
 }
@@ -439,17 +514,8 @@ bool RenderManager::Render( Integration::RenderStatus& status )
   // Core::Render documents that GL context must be current before calling Render
   DALI_ASSERT_DEBUG( mImpl->context.IsGlContextCreated() );
 
-  status.SetHasRendered( false );
-
   // Increment the frame count at the beginning of each frame
   ++(mImpl->frameCount);
-  mImpl->context.SetFrameCount(mImpl->frameCount);
-  mImpl->context.ClearRendererCount();
-  mImpl->context.ClearCulledCount();
-
-  PERF_MONITOR_START(PerformanceMonitor::DRAW_NODES);
-
-  SET_SNAPSHOT_FRAME_LOG_LEVEL;
 
   // Process messages queued during previous update
   mImpl->renderQueue.ProcessMessages( mImpl->renderBufferIndex );
@@ -495,12 +561,6 @@ bool RenderManager::Render( Integration::RenderStatus& status )
         RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i );
 
         DoRender( instruction, *mImpl->defaultShader );
-
-        const RenderListContainer::SizeType countRenderList = instruction.RenderListCount();
-        if ( countRenderList > 0 )
-        {
-          status.SetHasRendered( true );
-        }
       }
       GLenum attachments[] = { GL_DEPTH, GL_STENCIL };
       mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments);
@@ -511,13 +571,8 @@ bool RenderManager::Render( Integration::RenderStatus& status )
     }
   }
 
-  PERF_MONITOR_END(PerformanceMonitor::DRAW_NODES);
-
-  // check if anything has been posted to the update thread
-  bool updateRequired = !mImpl->resourcePostProcessQueue[ mImpl->renderBufferIndex ].empty();
-
   //Notify RenderGeometries that rendering has finished
-  for ( RenderGeometryOwnerIter iter = mImpl->renderGeometryContainer.Begin(); iter != mImpl->renderGeometryContainer.End(); ++iter )
+  for ( GeometryOwnerIter iter = mImpl->geometryContainer.Begin(); iter != mImpl->geometryContainer.End(); ++iter )
   {
     (*iter)->OnRenderFinished();
   }
@@ -531,10 +586,8 @@ bool RenderManager::Render( Integration::RenderStatus& status )
 
   DALI_PRINT_RENDER_END();
 
-  DALI_PRINT_RENDERER_COUNT(mImpl->frameCount, mImpl->context.GetRendererCount());
-  DALI_PRINT_CULL_COUNT(mImpl->frameCount, mImpl->context.GetCulledCount());
-
-  return updateRequired;
+  // check if anything has been posted to the update thread, if IsEmpty then no update required.
+  return !mImpl->textureUploadedQueue.IsEmpty();
 }
 
 void RenderManager::DoRender( RenderInstruction& instruction, Shader& defaultShader )
@@ -579,6 +632,20 @@ void RenderManager::DoRender( RenderInstruction& instruction, Shader& defaultSha
       return;
     }
   }
+  else if( instruction.mFrameBuffer != 0 )
+  {
+    instruction.mFrameBuffer->Bind( mImpl->context );
+    if ( instruction.mIsViewportSet )
+    {
+      // For glViewport the lower-left corner is (0,0)
+      const int y = ( instruction.mFrameBuffer->GetHeight() - instruction.mViewport.height ) - instruction.mViewport.y;
+      viewportRect.Set( instruction.mViewport.x,  y, instruction.mViewport.width, instruction.mViewport.height );
+    }
+    else
+    {
+      viewportRect.Set( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() );
+    }
+  }
   else // !(instruction.mOffscreenTexture)
   {
     // switch rendering to adaptor provided (default) buffer
@@ -628,7 +695,10 @@ void RenderManager::DoRender( RenderInstruction& instruction, Shader& defaultSha
 
   if( instruction.mRenderTracker && offscreen != NULL )
   {
-    instruction.mRenderTracker->CreateSyncObject();
+    // This will create a sync object every frame this render tracker
+    // is alive (though it should be now be created only for
+    // render-once render tasks)
+    instruction.mRenderTracker->CreateSyncObject( mImpl->glSyncAbstraction );
     instruction.mRenderTracker = NULL; // Only create once.
   }
 }