X-Git-Url: http://review.tizen.org/git/?a=blobdiff_plain;f=dali%2Finternal%2Frender%2Fcommon%2Frender-manager.cpp;h=35167edb7820eda9d808504597899397c0d3da54;hb=refs%2Fchanges%2F00%2F222500%2F6;hp=ee70d8943e74217107138a7c1362f8cbc80553d2;hpb=42f628136dc05b0d42069449b5826a024d5f8c9a;p=platform%2Fcore%2Fuifw%2Fdali-core.git diff --git a/dali/internal/render/common/render-manager.cpp b/dali/internal/render/common/render-manager.cpp index ee70d89..35167ed 100644 --- a/dali/internal/render/common/render-manager.cpp +++ b/dali/internal/render/common/render-manager.cpp @@ -18,13 +18,18 @@ // CLASS HEADER #include +// EXTERNAL INCLUDES +#include + // INTERNAL INCLUDES #include #include #include #include +#include #include #include +#include #include #include #include @@ -50,6 +55,13 @@ namespace Internal namespace SceneGraph { +#if defined(DEBUG_ENABLED) +namespace +{ +Debug::Filter* gLogFilter = Debug::Filter::New(Debug::NoLogging, false, "LOG_RENDER_MANAGER" ); +} // unnamed namespace +#endif + /** * Structure to contain internal data */ @@ -57,16 +69,17 @@ struct RenderManager::Impl { Impl( Integration::GlAbstraction& glAbstraction, Integration::GlSyncAbstraction& glSyncAbstraction, + Integration::GlContextHelperAbstraction& glContextHelperAbstraction, Integration::DepthBufferAvailable depthBufferAvailableParam, Integration::StencilBufferAvailable stencilBufferAvailableParam ) - : context( glAbstraction ), + : context( glAbstraction, &surfaceContextContainer ), currentContext( &context ), glAbstraction( glAbstraction ), glSyncAbstraction( glSyncAbstraction ), + glContextHelperAbstraction( glContextHelperAbstraction ), renderQueue(), instructions(), renderAlgorithms(), - backgroundColor( Dali::Stage::DEFAULT_BACKGROUND_COLOR ), frameCount( 0u ), renderBufferIndex( SceneGraphBuffers::INITIAL_UPDATE_BUFFER_INDEX ), defaultSurfaceRect(), @@ -79,10 +92,14 @@ struct RenderManager::Impl depthBufferAvailable( depthBufferAvailableParam ), stencilBufferAvailable( stencilBufferAvailableParam ) { + // Create thread pool with just one thread ( there may be a need to create more threads in the future ). + threadPool = std::unique_ptr( new Dali::ThreadPool() ); + threadPool->Initialize( 1u ); } ~Impl() { + threadPool.reset( nullptr ); // reset now to maintain correct destruction order } void AddRenderTracker( Render::RenderTracker* renderTracker ) @@ -122,6 +139,7 @@ struct RenderManager::Impl OwnerContainer< Context* > surfaceContextContainer; ///< List of owned contexts holding the GL state per surface Integration::GlAbstraction& glAbstraction; ///< GL abstraction Integration::GlSyncAbstraction& glSyncAbstraction; ///< GL sync abstraction + Integration::GlContextHelperAbstraction& glContextHelperAbstraction; ///< GL context helper abstraction RenderQueue renderQueue; ///< A message queue for receiving messages from the update-thread. // Render instructions describe what should be rendered during RenderManager::Render() @@ -129,8 +147,6 @@ struct RenderManager::Impl RenderInstructionContainer instructions; Render::RenderAlgorithms renderAlgorithms; ///< The RenderAlgorithms object is used to action the renders required by a RenderInstruction - Vector4 backgroundColor; ///< The glClear color used at the beginning of each frame. - uint32_t frameCount; ///< The current frame count BufferIndex renderBufferIndex; ///< The index of the buffer to read from; this is opposite of the "update" buffer @@ -152,16 +168,21 @@ struct RenderManager::Impl Integration::DepthBufferAvailable depthBufferAvailable; ///< Whether the depth buffer is available Integration::StencilBufferAvailable stencilBufferAvailable; ///< Whether the stencil buffer is available + std::unique_ptr threadPool; ///< The thread pool + Vector boundTextures; ///< The textures bound for rendering + Vector textureDependencyList; ///< The dependency list of binded textures }; RenderManager* RenderManager::New( Integration::GlAbstraction& glAbstraction, Integration::GlSyncAbstraction& glSyncAbstraction, + Integration::GlContextHelperAbstraction& glContextHelperAbstraction, Integration::DepthBufferAvailable depthBufferAvailable, Integration::StencilBufferAvailable stencilBufferAvailable ) { RenderManager* manager = new RenderManager; manager->mImpl = new Impl( glAbstraction, glSyncAbstraction, + glContextHelperAbstraction, depthBufferAvailable, stencilBufferAvailable ); return manager; @@ -225,11 +246,6 @@ RenderInstructionContainer& RenderManager::GetRenderInstructionContainer() return mImpl->instructions; } -void RenderManager::SetBackgroundColor( const Vector4& color ) -{ - mImpl->backgroundColor = color; -} - void RenderManager::SetDefaultSurfaceRect(const Rect& rect) { mImpl->defaultSurfaceRect = rect; @@ -435,7 +451,7 @@ ProgramCache* RenderManager::GetProgramCache() return &(mImpl->programController); } -void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) +void RenderManager::Render( Integration::RenderStatus& status, bool forceClear, bool uploadOnly ) { DALI_PRINT_RENDER_START( mImpl->renderBufferIndex ); @@ -451,21 +467,37 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) const uint32_t count = mImpl->instructions.Count( mImpl->renderBufferIndex ); const bool haveInstructions = count > 0u; + DALI_LOG_INFO( gLogFilter, Debug::General, + "Render: haveInstructions(%s) || mImpl->lastFrameWasRendered(%s) || forceClear(%s)\n", + haveInstructions ? "true" : "false", + mImpl->lastFrameWasRendered ? "true" : "false", + forceClear ? "true" : "false" ); + // Only render if we have instructions to render, or the last frame was rendered (and therefore a clear is required). if( haveInstructions || mImpl->lastFrameWasRendered || forceClear ) { - // Mark that we will require a post-render step to be performed (includes swap-buffers). - status.SetNeedsPostRender( true ); + DALI_LOG_INFO( gLogFilter, Debug::General, "Render: Processing\n" ); + + if ( !uploadOnly ) + { + // Mark that we will require a post-render step to be performed (includes swap-buffers). + status.SetNeedsPostRender( true ); + } // Switch to the shared context if ( mImpl->currentContext != &mImpl->context ) { mImpl->currentContext = &mImpl->context; + + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + } + // Clear the current cached program when the context is switched mImpl->programController.ClearCurrentProgram(); } - // Upload the geometries for( uint32_t i = 0; i < count; ++i ) { @@ -502,18 +534,26 @@ void RenderManager::Render( Integration::RenderStatus& status, bool forceClear ) } } - for( uint32_t i = 0; i < count; ++i ) + if ( !uploadOnly ) { - RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i ); + for( uint32_t i = 0; i < count; ++i ) + { + RenderInstruction& instruction = mImpl->instructions.At( mImpl->renderBufferIndex, i ); - DoRender( instruction ); - } + DoRender( instruction ); + } - GLenum attachments[] = { GL_DEPTH, GL_STENCIL }; - mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); - for ( auto&& context : mImpl->surfaceContextContainer ) - { - context->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + } + + GLenum attachments[] = { GL_DEPTH, GL_STENCIL }; + mImpl->context.InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); + for ( auto&& context : mImpl->surfaceContextContainer ) + { + context->InvalidateFramebuffer(GL_FRAMEBUFFER, 2, attachments); + } } //Notify RenderGeometries that rendering has finished @@ -557,7 +597,6 @@ void RenderManager::DoRender( RenderInstruction& instruction ) } Rect surfaceRect = mImpl->defaultSurfaceRect; - Vector4 backgroundColor = mImpl->backgroundColor; Integration::DepthBufferAvailable depthBufferAvailable = mImpl->depthBufferAvailable; Integration::StencilBufferAvailable stencilBufferAvailable = mImpl->stencilBufferAvailable; @@ -568,6 +607,12 @@ void RenderManager::DoRender( RenderInstruction& instruction ) { surfaceFrameBuffer = static_cast( instruction.mFrameBuffer ); + if ( !surfaceFrameBuffer->IsSurfaceValid() ) + { + // Skip rendering the frame buffer if the render surface becomes invalid + return; + } + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) { Context* surfaceContext = surfaceFrameBuffer->GetContext(); @@ -575,18 +620,24 @@ void RenderManager::DoRender( RenderInstruction& instruction ) { // Switch the correct context if rendering to a surface mImpl->currentContext = surfaceContext; + surfaceFrameBuffer->MakeContextCurrent(); + // Clear the current cached program when the context is switched mImpl->programController.ClearCurrentProgram(); } } surfaceRect = Rect( 0, 0, static_cast( surfaceFrameBuffer->GetWidth() ), static_cast( surfaceFrameBuffer->GetHeight() ) ); - backgroundColor = surfaceFrameBuffer->GetBackgroundColor(); } else { // Switch to shared context for off-screen buffer mImpl->currentContext = &mImpl->context; + + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + mImpl->glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + } } } @@ -599,6 +650,16 @@ void RenderManager::DoRender( RenderInstruction& instruction ) if( instruction.mFrameBuffer ) { instruction.mFrameBuffer->Bind( *mImpl->currentContext ); + + if ( !instruction.mFrameBuffer->IsSurfaceBacked() ) + { + // For each offscreen buffer, update the dependency list with the new texture id used by this frame buffer. + Render::TextureFrameBuffer* textureFrameBuffer = static_cast( instruction.mFrameBuffer ); + for (unsigned int i0 = 0, i1 = textureFrameBuffer->GetColorAttachmentCount(); i0 < i1; ++i0) + { + mImpl->textureDependencyList.PushBack( textureFrameBuffer->GetTextureId(i0) ); + } + } } else { @@ -611,20 +672,12 @@ void RenderManager::DoRender( RenderInstruction& instruction ) surfaceRect.y, surfaceRect.width, surfaceRect.height ); - - mImpl->currentContext->ClearColor( backgroundColor.r, - backgroundColor.g, - backgroundColor.b, - backgroundColor.a ); } // Clear the entire color, depth and stencil buffers for the default framebuffer, if required. // It is important to clear all 3 buffers when they are being used, for performance on deferred renderers // e.g. previously when the depth & stencil buffers were NOT cleared, it caused the DDK to exceed a "vertex count limit", // and then stall. That problem is only noticeable when rendering a large number of vertices per frame. - - mImpl->currentContext->SetScissorTest( false ); - GLbitfield clearMask = GL_COLOR_BUFFER_BIT; mImpl->currentContext->ColorMask( true ); @@ -642,8 +695,6 @@ void RenderManager::DoRender( RenderInstruction& instruction ) clearMask |= GL_STENCIL_BUFFER_BIT; } - mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR ); - if( !instruction.mIgnoreRenderToFbo && ( instruction.mFrameBuffer != 0 ) ) { if ( instruction.mFrameBuffer->IsSurfaceBacked() ) // Surface rendering @@ -696,29 +747,109 @@ void RenderManager::DoRender( RenderInstruction& instruction ) } } + bool clearFullFrameRect = true; + if( instruction.mFrameBuffer != 0 ) + { + Viewport frameRect( 0, 0, instruction.mFrameBuffer->GetWidth(), instruction.mFrameBuffer->GetHeight() ); + clearFullFrameRect = ( frameRect == viewportRect ); + } + else + { + clearFullFrameRect = ( surfaceRect == viewportRect ); + } + mImpl->currentContext->Viewport(viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height); + mImpl->currentContext->ClearColor( clearColor.r, + clearColor.g, + clearColor.b, + clearColor.a ); - if ( instruction.mIsClearColorSet ) + if( instruction.mIsClearColorSet && !clearFullFrameRect ) { - mImpl->currentContext->ClearColor( clearColor.r, - clearColor.g, - clearColor.b, - clearColor.a ); - - // Clear the viewport area only mImpl->currentContext->SetScissorTest( true ); mImpl->currentContext->Scissor( viewportRect.x, viewportRect.y, viewportRect.width, viewportRect.height ); - mImpl->currentContext->ColorMask( true ); - mImpl->currentContext->Clear( GL_COLOR_BUFFER_BIT , Context::CHECK_CACHED_VALUES ); + mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR ); mImpl->currentContext->SetScissorTest( false ); } + else + { + mImpl->currentContext->SetScissorTest( false ); + mImpl->currentContext->Clear( clearMask, Context::FORCE_CLEAR ); + } + + // Clear the list of bound textures + mImpl->boundTextures.Clear(); mImpl->renderAlgorithms.ProcessRenderInstruction( instruction, *mImpl->currentContext, mImpl->renderBufferIndex, depthBufferAvailable, - stencilBufferAvailable ); + stencilBufferAvailable, + mImpl->boundTextures ); + + // Synchronise the FBO/Texture access when there are multiple contexts + if ( mImpl->currentContext->IsSurfacelessContextSupported() ) + { + // Check whether any binded texture is in the dependency list + bool textureFound = false; + + if ( mImpl->boundTextures.Count() > 0u && mImpl->textureDependencyList.Count() > 0u ) + { + for ( auto textureId : mImpl->textureDependencyList ) + { + + textureFound = std::find_if( mImpl->boundTextures.Begin(), mImpl->boundTextures.End(), + [textureId]( GLuint id ) + { + return textureId == id; + } ) != mImpl->boundTextures.End(); + } + } + + if ( textureFound ) + { + + if ( !instruction.mFrameBuffer || !instruction.mFrameBuffer->IsSurfaceBacked() ) + { + // For off-screen buffer + + // Wait until all rendering calls for the currently context are executed + mImpl->glContextHelperAbstraction.WaitClient(); + + // Clear the dependency list + mImpl->textureDependencyList.Clear(); + } + else + { + // For surface-backed frame buffer + + // Worker thread lambda function + auto& glContextHelperAbstraction = mImpl->glContextHelperAbstraction; + auto workerFunction = [&glContextHelperAbstraction]( int workerThread ) + { + // Switch to the shared context in the worker thread + glContextHelperAbstraction.MakeSurfacelessContextCurrent(); + + // Wait until all rendering calls for the shared context are executed + glContextHelperAbstraction.WaitClient(); + + // Must clear the context in the worker thread + // Otherwise the shared context cannot be switched to from the render thread + glContextHelperAbstraction.MakeContextNull(); + }; + + auto future = mImpl->threadPool->SubmitTask( 0u, workerFunction ); + if ( future ) + { + mImpl->threadPool->Wait(); + + // Clear the dependency list + mImpl->textureDependencyList.Clear(); + } + } + } + } if( instruction.mRenderTracker && ( instruction.mFrameBuffer != 0 ) ) { @@ -733,6 +864,10 @@ void RenderManager::DoRender( RenderInstruction& instruction ) { surfaceFrameBuffer->PostRender(); } + else + { + mImpl->currentContext->Flush(); + } } } // namespace SceneGraph